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

Go does not use the C stack, so calling into C requires setting up a C stack, trampolining from the Go context to the C context, making the call and coming back.

As a result, while cgo is easy to use:

1. calling a C function from Go is ~100 times more expensive than calling a Go function from Go — or was a few years back anyway this may have improved a bit since: https://www.cockroachlabs.com/blog/the-cost-and-complexity-o...

2. and using cgo has semantics impact on the entire program: https://dave.cheney.net/2016/01/18/cgo-is-not-go

This is why Go libraries generally don't wrap native libraries and go software only calls into C when they really have no other choice, and/or have very "coalesced" APIs available (a single function call doing a lot of work whereas languages like Python are happy with making lots of very small calls to C).

And why Go itself does not use the platform-provided standard libraries and performs syscalls directly, breaking any time those syscalls change[0] — which is not that rare because linux is the only platforms where raw syscalls are the kernel's API, on most other systems the libc is the platform's API. And even on linux it breaks from time to time[1] because Go doesn't want to link to libc but still wants to benefit from vdso[2].

[0] https://github.com/golang/go/issues/16606

[1] https://marcan.st/2017/12/debugging-an-evil-go-runtime-bug/

[2] https://twitter.com/bcantrill/status/774290166164754433?lang...



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

Search: