Golang Cheatsheet

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

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 fails
    • require - exit current test if the assertion fails
  • Use httpexpect for API tests (if possible)

Compiling

Defer, recover panic avoid init() interfaces on caller side

Resources

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()