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.

Take quiz

Table of contents

  1. Why can't I add elements to my map?
    You can't assign an entry to a nil map in Go.
  2. What's a nil pointer dereference?
    You can't follow the nil pointer.
  3. Multiple values in single value context?
    If a function returns multiple values, you must use all of them.
  4. 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.
  5. Two variables with the same name?
    An identifier declared in a block may be redeclared in an inner block.
  6. Extra comma in slice literal
    In a multi-line slice, array or map literal, every line must end with a comma.
  7. Why can't I update my string?
    Go strings are read-only byte slices (with a few extra properties).
  8. Why aren't the characters concatenated?
    Characters (rune literals) are numbers in Go.
  9. What happened to ABBA?
    The Trim functions strip all Unicode code points contained in a cutset.
  10. What happened to my copy?
    Copy copies the minimum number of elements in the destination and source slices.
  11. Why doesn't append work every time?
    If there is enough capacity, append reuses the underlying array.
  12. 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.
  13. Why doesn't increment (++) and decrement (--) work?
    In Go increment and decrement are statements written with postfix notation.
  14. Why is my computation wrong?
    The multiplication, division, and remainder operators have the same precedence.
  15. Why does Go and Pythagoras disagree?
    The circumflex (^) denotes bitwise XOR in Go.
  16. 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.
  17. Numbers that start with zero
    Octal literals start with 0, hexadecimal with 0x, an decimal literals never start with zero.
  18. What's wrong with the remainder (modulo) operator?
    The remainder operator can give negative answers if the dividend is negative.
  19. Why can't I multiply a time.Duration with an integer?
    There is no mixing of numeric types in Go.
  20. Why is this index out of range?
    Arrays, slices and strings are indexed starting from zero.
  21. Unexpected values in range loop
    The range loop generates two values: first the index, then the data.
  22. Can't change entries in range loop
    The range loop uses a local variable to store iteration values.
  23. Iteration variable doesn't see change in range loop
    The range expression is evaluated once before beginning the loop.
  24. 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.
  25. Why is the JSON output empty?
    Only the the exported fields of a Go struct will be present in the JSON output.
  26. Why is nil not equal to nil?
    An interface value is nil only if the concrete value and dynamic type are both nil.