Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Access map element (that does not exist)

  • What happens when we access an element that does not exist?
  • Go returns 0.
  • So we can't know if the field exists and its value is 0 or if it does not exist at all.
package main

import "fmt"

func main() {
	grades := make(map[string]int)

	grades["Mary"] = 99
	grades["Joe"] = 0
	fmt.Println(grades["Mary"])
	fmt.Println(grades["Joe"])
	fmt.Println(grades["Jane"])

}
99
0
0