4 ms·
Once you use CGO, portability is gone. Your binary is no longer staticly compiled. This can happen subtley without you knowing it. If you use a function in the
by bitbasher 10mo ago
Once you use CGO, portability is gone. Your binary is no longer staticly compiled.
This can happen subtley without you knowing it. If you use a function in the standard library that happens to call into a CGO function, you are no longer static.
This happens with things like os.UserHomeDir or some networking things like DNS lookups.
You can "force" go to do static compiling by disabling CGO, but that means you can't use _any_ CGO. Which may not work if you require it for certain things like sqlite.
- swills 10mo agoYou can definitely use CGO and still build statically, but you do need to set ldflags to include -static.
- ncruces 10mo agoThere are at least a couple of ways to run SQLite without CGO.
- silverwind 10mo ago> This happens with things like os.UserHomeDir or some networking things like DNS lookups. The docs do not mention this CGO dependency, are you sure? https://pkg.go.dev/os#UserHomeDir https://pkg.go.dev/os#UserHomeDir
- purpleidea 10mo agoI was surprised too, that I had to check the docs, so I assume the user was misinformed.
- bitbasher 10mo agoPerhaps I misremembered or things changed? For instance, the os/user results in a dynamicly linked executable: https://play.golang.com/p/7QsmcjJI4H5 https://play.golang.com/p/7QsmcjJI4H5 There are multiple standard library functions that do it.. I recall some in "net" and some in "os".
- telotortium 10mo agoos.UserHomeDir is specified to read the HOME environment variable, so it doesn’t require CGo. os/user does, but only to support NSS and LDAP, which are provided by libc. That’s also why net requires CGo- for getaddrinfo using resolv.conf
- PunchyHamster 10mo ago> Which may not work if you require it for certain things like sqlite. there is cgo-less sqlite implementation https://github.com/glebarez/go-sqlite https://github.com/glebarez/go-sqlite it seems to not be maintained much tho
- hiAndrewQuinn 10mo agoYou don't need CGO for SQLite in most cases; I did a deep dive into it here. https://til.andrew-quinn.me/posts/you-don-t-need-cgo-to-use-sqlite-in-your-go-binary/ https://til.andrew-quinn.me/posts/you-don-t-need-cgo-to-use-...