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

Map element exists

  • The variable name ok is not speacial. It is just a convention.
package main

import "fmt"

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

	grades["Mary"] = 99
	grades["Joe"] = 0

	value, ok := grades["Mary"]
	fmt.Println(ok)
	fmt.Println(value)
	fmt.Println()

	value, ok = grades["Joe"]
	fmt.Println(ok)
	fmt.Println(value)
	fmt.Println()

	value, ok = grades["Jane"]
	fmt.Println(ok)
	fmt.Println(value)
}
true
99

true
0

false
0