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 usemake()
. - When coding a
String()
method for a type, don’t usefmt.Print
or alike in the code, use thefmt.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.