To produce a pure statically linked binary, is it still necessary to compile with -tags netgo in Go 1.5+?

huangapple go评论92阅读模式
英文:

To produce a pure statically linked binary, is it still necessary to compile with -tags netgo in Go 1.5+?

问题

我正在尝试创建一个纯静态链接的二进制文件,以在一个最小化的Docker容器中运行。在Go 1.5之前,我是这样构建它们的:

go build -a -tags netgo -installsuffix netgo myfile.go

我了解到在Go 1.5版本中,Go编译器已经去除了C语言支持。是否仍然需要使用-tags-installsuffix来构建呢?

英文:

I'm trying to create a pure statically linked binary to run in a minimal Docker container. Prior to Go 1.5, I was building them like this:

go build -a -tags netgo -installsuffix netgo myfile.go

I understand that C has been stripped out of the Go compiler in version 1.5. Is it still necessary to build with -tags and -installsuffix?

答案1

得分: 9

在Go 1.5版本中,net包中的DNS解析器几乎总是使用cgo来访问系统接口。但是在Go 1.5中的一项改变意味着,在大多数Unix系统上,DNS解析将不再需要cgo,这简化了在这些平台上的执行。现在,如果系统的网络配置允许,原生的Go解析器就足够了。这个改变的重要影响是,每个DNS解析占用一个goroutine而不是一个线程,因此具有多个未完成的DNS请求的程序将消耗更少的操作系统资源。

决定如何运行解析器是在运行时而不是构建时应用的。不再需要使用netgo构建标签来强制使用Go解析器,尽管它仍然有效。新的netcgo构建标签可以在构建时强制使用cgo解析器。要在运行时强制使用cgo解析,请在环境中设置GODEBUG=netdns=cgo。更多调试选项可以在这里找到。

这个改变只适用于Unix系统。Windows、Mac OS X和Plan 9系统的行为与以前相同。

英文:

https://golang.org/doc/go1.5#net

> The DNS resolver in the net package has almost always used cgo to
> access the system interface. A change in Go 1.5 means that on most
> Unix systems DNS resolution will no longer require cgo, which
> simplifies execution on those platforms. Now, if the system's
> networking configuration permits, the native Go resolver will suffice.
> The important effect of this change is that each DNS resolution
> occupies a goroutine rather than a thread, so a program with multiple
> outstanding DNS requests will consume fewer operating system
> resources.
>
> The decision of how to run the resolver applies at run time, not build
> time. The netgo build tag that has been used to enforce the use of the
> Go resolver is no longer necessary
, although it still works. A new
> netcgo build tag forces the use of the cgo resolver at build time. To
> force cgo resolution at run time set GODEBUG=netdns=cgo in the
> environment. More debug options are documented here.
>
> This change applies to Unix systems only. Windows, Mac OS X, and Plan
> 9 systems behave as before.

So no.

huangapple
  • 本文由 发表于 2015年9月6日 01:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/32416079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定