67

“完全静态编译一个Go程序"

 5 years ago
source link: http://colobu.com/2018/07/20/totally-static-Go-builds/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在Docker化的今天, 我们经常需要静态编译一个Go程序,以便方便放在Docker容器中。 即使你没有引用其它的第三方包,只是在程序中使用了标准库 net ,你也会发现你编译后的程序依赖glic,这时候你需要glibc-static库,并且静态连接。

不同的Go版本下静态编译方式还有点不同,在go 1.10下, 下面的方式会尽可能做到静态编译:

CGO_ENABLED=0 go build -a -ldflags '-extldflags "-static"' .

有一个 提案 请求给编译加一个 static ,如果接收了的话也许在将来的go中直接使用 static

参考文档

  1. http://blog.wrouesnel.com/articles/Totally%20static%20Go%20builds/
  2. https://github.com/golang/go/issues/9344
  3. https://github.com/golang/go/issues/26492

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK