Avoiding common Go mistakes
This section will briefly talk about some common Go mistakes so that you can avoid them in your programs:
- If you have an error in a Go function, either log it or return it; do not do both unless you have a really good reason to do so.
- Go interfaces define behaviors, not data and data structures.
- Use the
io.Reader
andio.Writer
interfaces because they make your code more extensible. - Make sure that you pass a pointer to a variable to a function only when needed. The rest of the time, just pass the value of the variable.
- Error variables are not strings; they are
error
values. - If you are afraid of making mistakes, you will most likely end up doing nothing useful. So experiment as much as you can.
The following are general pieces of advice that can be applied in every programming language:
- Test your Go code and functions in small and autonomous Go programs to make sure that they behave the way you think they should
- If you do not really know a Go feature, test it before using...