Go gotcha
26 pitfalls and corner cases
We hope this collection of common mistakes in Go will help you spot similar traps and save you time when debugging your own code.
Table of contents
-
Go gotcha: Why can't I add elements to my map?
You can't assign an entry to a nil map in Go.
-
Go gotcha: What's a nil pointer dereference?
You can't follow the nil pointer.
-
Go gotcha: Multiple values in single value context?
If a function returns multiple values, you must use all of them.
-
Go gotcha: Why doesn't this function change my array?
Arrays in Go are values: when you pass an array to a function it gets a copy of the original array data.
-
Go gotcha: Two variables with the same name?
An identifier declared in a block may be redeclared in an inner block.
-
Go gotcha: Extra comma in slice literal
In a multi-line slice, array or map literal, every line must end with a comma.
-
Go gotcha: Why can't I update my string?
Go strings are read-only byte slices (with a few extra properties).
-
Go gotcha: Why aren't the characters concatenated?
Characters (rune literals) are numbers in Go.
-
Go gotcha: What happened to ABBA?
The Trim functions strip all Unicode code points contained in a cutset.
-
Go gotcha: What happened to my copy?
Copy copies the minimum number of elements in the destination and source slices.
-
Go gotcha: Why doesn't append work every time?
If there is enough capacity, append reuses the underlying array.
-
Go gotcha: Why can't I print large numbers? (constant overflows int)
An untyped integer constant is converted to an int when the type can't be inferred from the context.
-
Go gotcha: Why doesn't increment (++) and decrement (--) work?
In Go increment and decrement are statements written with postfix notation.
-
Go gotcha: Why is my computation wrong?
The multiplication, division, and remainder operators have the same precedence.
-
Go gotcha: Why does Go and Pythagoras disagree?
The circumflex (^) denotes bitwise XOR in Go.
-
Go gotcha: Why doesn't this loop end?
An integer overflow occurs when an arithmetic operation tries to create a value that is outside the range that can be represented.
-
Go gotcha: Numbers that start with zero
Octal literals start with 0, hexadecimal with 0x, an decimal literals never start with zero.
-
Go gotcha: What's wrong with the remainder (modulo) operator?
The remainder operator can give negative answers if the dividend is negative.
-
Go gotcha: Why can't I multiply a time.Duration with an integer?
There is no mixing of numeric types in Go.
-
Go gotcha: Why is this index out of range?
Arrays, slices and strings are indexed starting from zero.
-
Go gotcha: Unexpected values in range loop
The range loop generates two values: first the index, then the data.
-
Go gotcha: Can't change entries in range loop
The range loop uses a local variable to store iteration values.
-
Go gotcha: Iteration variable doesn't see change in range loop
The range expression is evaluated once before beginning the loop.
-
Go gotcha: Iteration variables and closures
A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write.
-
Go gotcha: Why is the JSON output empty?
Only the the exported fields of a Go struct will be present in the JSON output.
-
Go gotcha: Why is nil not equal to nil?
An interface value is nil only if the concrete value and dynamic type are both nil.