logoalt Hacker News

bitbasherlast Saturday at 12:40 AM5 repliesview on HN

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.


Replies

swillslast Saturday at 1:08 AM

You can definitely use CGO and still build statically, but you do need to set ldflags to include -static.

show 1 reply
hiAndrewQuinnlast Saturday at 7:17 AM

You 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-...

PunchyHamsterlast Saturday at 7:04 AM

> 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 it seems to not be maintained much tho

show 1 reply
silverwindlast Saturday at 1:41 AM

> 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

show 1 reply
ncruceslast Saturday at 1:10 AM

There are at least a couple of ways to run SQLite without CGO.

show 1 reply