3 ms·
> Golang doesn’t use libc on Linux, and instead calls syscalls directly Could someone explain this. I'm not familiar with low level linux stuff. Why would you
by zasdffaa 4y ago
> Golang doesn’t use libc on Linux, and instead calls syscalls directly
Could someone explain this. I'm not familiar with low level linux stuff. Why would you choose not to use libc, what are the implications?
- aviramha 4y agoHey, One of the writers of the article here. This is a very controversial topic in Go ecosystem, and following all the discussions that led to this decision is very hard. In short, I think main reasons are: 1. libc is considered hazard - security, comfort, runtime. It needs to maintain support for so many flows and setups so it's hard to make things better. For example, having `errno` as a global is quite weird (instead of just returning it, which is a whole discussion of it's own) 2. Golang devs like to re-invent the wheel, in a kind of Apple-ish way - all other are doing it wrong, we're going to do it better. ofc it's debatable whether they're correct with their approach. 3. Given they use Plan 9 system design, doing FFI from Go is very expensive (need to switch stack, save context, etc on each call)
- infiniteregrets 4y agoA relevant discussion: https://news.ycombinator.com/item?id=25997506 https://news.ycombinator.com/item?id=25997506
- metadat 4y ago^ (Go 1.16 will make system calls through Libc on OpenBSD)
- tialaramex 4y ago> For example, having `errno` as a global is quite weird It was quite weird before we wrote concurrent software. Once you're writing concurrent software it's very weird because as originally documented this single error location is shared by all threads, so it necessarily gets concurrently modified while being read, a Data Race. Your 2022 C library typically defines it as Thread Local instead, so when you call some_c_function() and then check errno, you aren't confused by the fact meanwhile another thread tried to open("nonexistent.file") and so their thread set errno to report that the file doesn't exist. Given that none of this fuss is how the actual system calls work, it's understandable that people might want to sidestep it.
- aviramha 4y agoThanks for elaborating! I totally agree there's valid reasons to drop libc, though not sure if that's the right way. In most modern languages today you can just return a tuple, not needing to mess with TLS.. whereas old times didn't really like passing results in out parameters (by ref) (which makes sense, it's less ergonomic).
- throwaway894345 4y agoI much prefer the ergonomics of passing out-parameters than TLS. Honestly, TLS has always struck me as a code smell in any language.
- throwaway894345 4y ago> Golang devs like to re-invent the wheel, in a kind of Apple-ish way - all other are doing it wrong, we're going to do it better. ofc it's debatable whether they're correct with their approach. I'm very happy with these tradeoffs. Not only is it a pleasure not to have to deal with libc (truly static binaries on Linux, 2mb docker images, etc), but because interop is tedious/expensive, the ecosystem is largely pure-Go. This means that we rarely (if ever) have to deal with C's abysmal build tooling and (lack of) package managers and stuff like cross compilation is trivial. Everything builds consistently on every system, every time, and irrespective of target platform (provided there are no C dependencies).
- aviramha 4y agoMy concern here is that many people are now writing software in Go which can only be used from Go. Yes, you can cgo but as you wrote the interop is tedious/expensive. It’s good we’re rewriting old software but this time in a semi closed garden…
- throwaway894345 4y agoI mean, the garden has always been semi-closed, we've never had a truly open garden, and nothing Go did was going to change that. In other words, what language has ever solved the problem of cross-language-reuse in a way that was so simple that we didn't end up rewriting a huge swath of the ecosystem in every new language? It's not like Java or Rust came along and now we can stop rewriting HTTP libraries in subsequent languages. Easy interop has only ever been an illusion (we forget about the difficulties of integrating distinct build systems and memory management systems or the runtime costs of marshaling data between memory layouts). Rather than paying significant costs to preserve that illusion, Go doubled down in order to realize some pretty considerable gains: easy compilation even on niche distros, even when cross compiling, truly static binaries, etc.
- aviramha 4y agoWe’re sliding into opinions but using C, Python, Rust in each interop had some cost but I did it and got a lot of benefits of using existing good software. Yes C lacks dev ergonomics, but meson + ninja gave a very neat experience..
- rapidlua 4y agoThis is mostly due to ease of distribution. The binary is self-contained, no need to ship anything but the binary itself. You can also compile locally and then run it elsewhere that is a completely different flavor of Linux. Quite handy when experimenting. Finally, no dependency on libc enables easy cross-compilation. You might not even have libc installed for the architecture you are targeting!