Remix.run Logo
bitbasher 4 days 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 4 days ago | parent | next [-]

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

tptacek 4 days ago | parent [-]

You can even cross-compile doing that.

swills 4 days ago | parent [-]

Yes, indeed, I do.

hiAndrewQuinn 4 days ago | parent | prev | next [-]

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

PunchyHamster 4 days ago | parent | prev | next [-]

> 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

IceWreck 4 days ago | parent [-]

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

debugnik 4 days ago | parent [-]

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

silverwind 4 days ago | parent | prev | next [-]

> 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

purpleidea 4 days ago | parent [-]

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

bitbasher 4 days ago | parent [-]

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

telotortium 3 days ago | parent [-]

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

ncruces 4 days ago | parent | prev [-]

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

tptacek 4 days ago | parent [-]

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

apitman 4 days ago | parent [-]

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

ncruces 3 days ago | parent [-]

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