Last active
December 11, 2015 06:39
-
-
Save macu/4561066 to your computer and use it in GitHub Desktop.
Go language tips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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