- The Zen of Go
- https://antonz.org/which-go/
- A map can always grow in memory; it never shrinks. Hence, if it leads to some memory issues, you can try different options, such as forcing Go to re-create the map or using pointers. - https://100go.co/28-maps-memory-leaks/
- Go style guide: https://google.github.io/styleguide/go/
- Range produces a copy
- Context - https://go.dev/blog/context
- Best practices - https://go.dev/talks/2013/bestpractices.slide
- Faster Go code by being mindful of memory
- https://blog.carlmjohnson.net/post/2021/how-to-use-go-embed/
- https://vincent.bernat.ch/en/blog/2023-dynamic-protobuf-golang
- https://olegk.dev/github-actions-and-go#heading-intro
- https://commandcenter.blogspot.com/2012/06/less-is-exponentially-more.html
- https://www.convictional.com/blog/go-embed
- https://journal.stuffwithstuff.com/2023/10/19/does-go-have-subtyping/
- https://rednafi.com/go/slice_gotchas/
- https://blog.cloud66.com/our-golang-stack
Benefits
- Type safety because statically typed, less bugs at runtime
- faster build, faster speed of development and feedback
- smaller containers = faster deploy, less memory usage
- simpler code, no magic
- performance
Concurrency
- Concurrency - https://www.storj.io/blog/production-concurrency
- buffered channels
- sync.Mutex, sync.RWMutex
Frameworks
- Go comes with pretty much everything needed to build APIs
- Use chi - It is 100% compatible with net/http, is idiomatic, has been around for a long time and is maintained. Only really used for routing.
- Consider using Echo (if needed).
JSON
- Decoder vs Unmarshal? - io.Reader use decoder, but potential bugs
Logging
- Use structured logging to reduce allocations
- Add contextual information to the logger
Testing
- Use testify for assertions
assert
- continue test if the assertion failsrequire
- exit current test if the assertion fails
- Use httpexpect for API tests (if possible)
Compiling
- https://mt165.co.uk/blog/static-link-go/
- https://www.arp242.net/static-go.html
- https://www.natarajmb.com/2021/02/cross-compiling-rust-using-docker/
- https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
- https://www.cloudbees.com/blog/building-minimal-docker-containers-for-go-applications
- https://github.com/golang/go/issues/28966
- https://github.com/golang/go/issues/47175
- https://pkg.go.dev/cmd/cgo
- https://github.com/golang/go/wiki/GccgoCrossCompilation
- https://www.uber.com/en-BG/blog/go-monorepo-bazel/
Defer, recover panic avoid init() interfaces on caller side
Resources
- https://go.dev/tour/ (beginner)
- https://gobyexample.com/
- https://go.dev/doc/effective_go (tips for writing clear, idiomatic Go code
- https://about.sourcegraph.com/blog/go/idiomatic-go
- https://github.com/teivah/100-go-mistakes/blob/master/README.md (advanced)
Companies
- Seldon ML
- Form3
- Utility warehouse
- Luno
- Bud
- Salt Pay
- Utopian music
Unordered
Slowloris Attack - limit connections RUDY dos attack - set timeout Use goreleaser?
context.WithTimeout() context.WithCancel() context.WithoutCancel() Time.NewTicker() var wg := sync.WaitGroup wg.Add()