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