This chapter covers
- Variable shadowing
- Logging side effects
- Comparing values
- Preventing common JSON mistakes
- Formatting network addresses
- Handling enums
- Using
deferand understanding how arguments are handled
- Releasing resources
- Octal literal
- Integer overflow
- Using linters
In this chapter, we are going to cover various basic mistakes made by Go developers. For example, when it comes to implementing HTTP services in Go, we have to know how to prevent common JSON mistakes, format network addresses, and release HTTP body resources using defer efficiently. We will also see some basic mistakes that all developers may have already made, such as unintended variable shadowing, how to handle value comparisons in Go, or not using linters. We will delve into each mistake and explain when we can face such problems and the possible solutions to overcome them.
The scope of a variable refers to its visibility. In other words, the part of the program where a name binding is valid. In Go, a variable name declared in a block may be redeclared in an inner block. This principle, called variable shadowing, is prone to common mistakes.
In the following example, we will see an occurence of a bug because of a shadowed variable. We will create an HTTP client in two different ways, depending on the value of a tracing boolean: