Phone Book

Look up a contact by name using the comma-ok pattern.

package main

import "fmt"

func main() {
	phoneBook := map[string]string{
		"Alice": "555-1234",
		"Bob":   "555-5678",
		"Carol": "555-9012",
	}

	var name string
	fmt.Print("Look up: ")
	fmt.Scan(&name)

	if number, ok := phoneBook[name]; ok {
		fmt.Printf("%s's number: %s\n", name, number)
	} else {
		fmt.Printf("%s not found.\n", name)
	}
}

💻 Run locally

Copy the code above and run it on your machine

© 2026 ByteLearn.dev. Free courses for developers. · Privacy