Skip to content

Instantly share code, notes, and snippets.

@pwittrock
Last active October 31, 2023 04:27
Show Gist options
  • Save pwittrock/e4dc462720584d6a487b to your computer and use it in GitHub Desktop.
Save pwittrock/e4dc462720584d6a487b to your computer and use it in GitHub Desktop.
GOLANG Common Bugs
  1. for _, value := range values { go func() {somefn(value)}() } // Every go func sees that same value because the address is updated
  • Must copy variable in a for loop to new value if using in a gofn, address of variable get reused
  1. m := map[string]SomeStruct &m["k"] // Fails to compile
  • Cannot take the address of map keys/values: use a pointer when possible
  1. err := somefn() if err != nil { newvar, err := somefn() } // Does not update err in outer scope!
  • must declare var newvar string and then use newvar, err = somefn()
  1. go f1(blockingFn()) // blockingFn() not run in a go routine - equivalent to r := blockingFn() go f1(r)
  • blockingFn() is run synchronously outside a go routine, and f1 is run in a go routine with the realized argument
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment