Most common mistakes I’ve seen in Go projects

  • Unknown enum value. As a best practice, set the unknown value of an enum to 0 to avoid errors.
  • Relying too much on pointers for speed. Passing a variable by value creates a copy of the variable but passing the pointer only passes the memory address, so it should be faster. However, by default, create functions to use variables. Use pointers only to share variables.
  • Misunderstanding context. Context is composable and can carry a deadline, cancelation signal or another value across API boundaries. Make use of the fact that you can compose the context to both carry a signal and other values, and that multiple goroutines can share the same context (thus you can cancel all of them at once).
  • Not using the -race option. Always enable this to reduce concurrency issues when testing.
  • Using a filename as input. Make use of the io.Reader and io.Writer abstractions instead.

Full post here, 11 mins read