Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



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


You can even cross-compile doing that.


Yes, indeed, I do.


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


> 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


You're linking to a different version - this is the one that most people use https://github.com/modernc-org/sqlite


Yes and no, the package above is a popular `database/sql` driver for the same SQLite port you linked.


> 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


I was surprised too, that I had to check the docs, so I assume the user was misinformed.


Perhaps I misremembered or things changed? For instance, the os/user results in a dynamicly linked executable: https://play.golang.com/p/7QsmcjJI4H5

There are multiple standard library functions that do it.. I recall some in "net" and some in "os".


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


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


I think the standard answer here is modernc.org/sqlite.


Careful, you're responding to the author of a wasm-based alternative.


No need to be careful. I won't bite. ;)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: