12 ms·
Is there an official documented rationale for why the Go standard library doesn't go use libc, and instead does all this direct syscall stuff? I've heard vario
by twic 8y ago
Is there an official documented rationale for why the Go standard library doesn't go use libc, and instead does all this direct syscall stuff?
I've heard various stories about it over the years, so i would appreciate an authoritative source.
- steveklabnik 8y agoMy understanding, which is certainly not authoritative, is that Go wants to be 100% statically linked. libc is usually dynamically linked. MUSL is Linux only.
- oconnor663 8y agoIt often does use libc. OP's article mentioned that they're using it more on Darwin now, but they've also been using it on Linux for a long time to do some of their DNS resolution (where implementing a stable parser for /etc/resolv.conf and friends is nobody's idea of a good time). I don't have an authoritative source, but I think one of the big downsides of CGO for them is that it hurts their cross-compilation story. Cross-compiling a pure Go program is trivial, which is great, but by default it disables CGO.
- cube2222 8y agoExactly, it disables CGO. So if you cross compile Windows -> Linux the pure Go DNS resolution code gets used.
- valarauca1 8y agowhy the Go standard library doesn't go use libc They do you use a `libc` they use `musl-libc` [1] as apposed to `gnu-libc` (gblic) [2]. There are some differences [3], the largest (without getting into various API incompatibilities) is licensing. `musl-libc` is BSD licensed, while `gnu-libc` (glibc) is GPL licensed. The parent post does explain, about as well as any member of the go-team has to date for their usage of `musl-libc` over `gnu-libc` (glibc). Overall `musl-libc` does focus on correctness a lot more then `glibc`, and static linking is a large benefit. Static linking isn't an issue when you expect applications to be short lived transient items. Where system images only exist for days, or hours before being redeployed by either patches, or CI-Managered triggered updates. I think this is the largest disconnect. Go expects the development pipeline to be quick so nobody expects multi-year lifespan binaries, which is directly contradictory to how many expect executable binaries to behave. No that I'm authority on this, just stating a common disconnect I've experienced working with ex-Googlers. [1] https://www.musl-libc.org/ https://www.musl-libc.org/ [2] https://www.gnu.org/software/libc/ https://www.gnu.org/software/libc/ [3] https://wiki.musl-libc.org/functional-differences-from-glibc.html https://wiki.musl-libc.org/functional-differences-from-glibc... [4] https://github.com/golang/go/issues/9627 https://github.com/golang/go/issues/9627
- masklinn 8y ago> They do you use a `libc` they use `musl-libc` [1] as apposed to `gnu-libc` (gblic) [2] No. Historically they don't use a libc at all if you don't use the DNS subsystem. The latest version (1.11) finally started using libSystem on OSX (where performing raw syscalls has never ever been supported, and Go broke several time because they refused to acknowledge that).
- valarauca1 8y agoMy Go v1.11 compiler still emits symbols from `musl-libc` for static builds on linux. I had to dig through on object dump just on friday.
- TheDong 8y agoAre you running on a linux distro which uses musl, such as alpine? Are you running the build inside a docker container which uses musl, such as alpine? Those are the cases where this might happen. On a normal linux distro, this will not happen.
- TheDong 8y agoThis post is just plain wrong. They do not default to musl on any platform. They only default to the system's libc (which is typically gnu libc, though could be musl) if certain networking constructs are used. They otherwise default to not using any external libc library, but rather implementing one with syscalls internally. The above is for linux. On other platforms it varies further, but to my knowledge the parent post is not right on any platform.
- pcwalton 8y agoPerformance, most likely. Go wants small stacks, which requires all dependent libraries to be compiled by the Go toolchain, or else suffer performance consequences due to stack switches. libc obviously wasn't compiled with the Go toolchain, so calling it takes the performance hit of stack switching.
- shanemhansen 8y agoI think the key to the rationale is in the name. libc is a library for C. Go doesn't use the same calling conventions as platform C and does a number of things that make it not really play well with C code. The authors want a world where you can just make a read syscall without using FFI. It's possible this is a unrealistic expectation. Linux kernel interfaces are generally quite stable. On OSX and windows the kernel interface seems to be an undocumented unsupported API which is sad.
- tom_ 8y agoThe Windows programming interface comes in the shape of the system DLLs - you load kernel32.dll or whatever, find the symbols you want, and call them. No C necessary, and the interfaces never change. (You might need some stack space to call the functions though.)