Common Pitfalls and Best Practices in Go

David Lee
3 min readMar 15, 2024

Here I have concluded with some pitfalls and best practices in Go, use ChatGPT to help you if you find it hard to understand ;)

Common Pitfalls

Here is an overview of pitfalls for your convenience, referring to where you can find more explanations and examples:

  • Never use var p*a to not confuse pointer declaration and multiplication.
  • Never change the counter-variable within the for-loop itself.
  • Never use the value in a for-range loop to change the value itself.
  • Never use goto with a preceding label.
  • Never forget the braces () after a function name to call the function specifically when calling a method on a receiver or invoking a lambda function as a Goroutine.
  • Never use new() with maps, always use make().
  • When coding a String() method for a type, don’t use fmt.Print or alike in the code, use the fmt.Sprint family of methods.
  • Never forget to use Flush() when terminating buffered writing.
  • Never ignore errors because ignoring them can lead to program crashes.
  • Do not use global variables or shared memory; they make your code unsafe for running concurrently.

--

--

David Lee

Gopher and Rustacean -- > ⾶ 🤺 I use VIM btw 🤫