Cgo allows to bridge C and Go. From C, we can connect to other languages, like C++, too. Use cases are legacy code or performance. Implementing a pseudo-random walk (using an LCG) in both Go and C shows that C can be up to 4x faster for this particular task.
C 1 -1 29.553µs GO 1 -1 84.303µs C 1000 -12 41.799µs GO 1000 26 107.295µs C 1000000 -1810 22.837387ms GO 1000000 -2 83.182797ms C 1000000000 15894 6.25975724s GO 1000000000 -44632 25.620427056s
Experiment details can be found here: https://github.com/miku/cgosamples
The zig compiler (LLVM) can build real staticly linked binaries from a Go project with C dependencies, such as sqlite3. Example repo: https://github.com/klingtnet/cross-compile-with-zigcc.
- Cgo is a not Go, many guarantees are given up, maintenance of mixed C and Go code is much more difficult - rather than use CGO, maybe a grpc or other interface may keep C and Go world better separated
- To wrap C++, we need to create a C façade
- Go has a nice ast
- GRPC performance in Go may suffer, if interfaces are used (due to overhead of reflection)
- oapi-codegen is an openapi code generator for Go (and an alternative to the swagger tool)
- Go’s type system can be limiting, when working with Web APIs or SDKs
- Simple Markup Language
- Parsec, Participle
- Using symbols straight from a dynamic library via dlopen
- A rundown of a benchmark across Python, C, and BLAS, for a HPC and competetive programming blog: https://en.algorithmica.org/hpc/complexity/languages/
- Unbuffered terminal output in Go is slow. Try to wrap
bufio.Writerif you need to print a lot of text.
Did you find a nice haiku?
Join our meetup and let us know!