Skip to content

Instantly share code, notes, and snippets.

@macu
Last active December 11, 2015 06:39
Show Gist options
  • Select an option

  • Save macu/4561066 to your computer and use it in GitHub Desktop.

Select an option

Save macu/4561066 to your computer and use it in GitHub Desktop.
Go language tips
package main
import "fmt"
func main() {
var m map[int]string
fmt.Printf("m = %v; m == nil? %t\n", m, m == nil)
// it is safe to range over a nil map
for i, v := range m {
fmt.Printf("map[%d] = %v\n", i, v)
}
// it is safe to access a nil map
a, ok := m[1]
fmt.Printf("a = %q, defined = %t\n", a, ok)
// but unsafe to assign
m[1] = "new value" // raises runtime error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment