Generics Basics
Map Key Helper
The comparable constraint allows a generic helper to use map keys.
comparable
Map keys must be comparable, so a generic map helper can require `K comparable`. The trace uses a concrete string-keyed map and shows the generic shape in a comment.
Map Key Helper
map_key_helper.go
Replay: real traced execution (multi-file project)
package main
import "fmt"
// Generic shape: Lookup[K comparable, V any](items map[K]V, key K) (V, bool)
func LookupScore(items map[string]int, key string) (int, bool) {
value, ok := items[key]
return value, ok
}
func main() {
var key = "beta"
scores := map[string]int{"alpha": 10, "beta": 20}
value, ok := LookupScore(scores, key)
fmt.Println("key=", key)
fmt.Println("found=", ok)
fmt.Println("value=", value)
}
package main
import "fmt"
// Generic shape: Lookup[K comparable, V any](items map[K]V, key K) (V, bool)
func LookupScore(items map[string]int, key string) (int, bool) {
value, ok := items[key]
return value, ok
}
func main() {
var key = "alpha"
scores := map[string]int{"alpha": 10, "beta": 20}
value, ok := LookupScore(scores, key)
fmt.Println("key=", key)
fmt.Println("found=", ok)
fmt.Println("value=", value)
}
package main
import "fmt"
// Generic shape: Lookup[K comparable, V any](items map[K]V, key K) (V, bool)
func LookupScore(items map[string]int, key string) (int, bool) {
value, ok := items[key]
return value, ok
}
func main() {
var key = "delta"
scores := map[string]int{"alpha": 10, "beta": 20}
value, ok := LookupScore(scores, key)
fmt.Println("key=", key)
fmt.Println("found=", ok)
fmt.Println("value=", value)
}
key ← "beta", scores ← map[string]int{"alpha":10, "beta":20}
11func main() {12 var key→ "beta" = "beta" //@key="alpha", "delta"13 scores→ map[string]int{"alpha":10, "beta":20} := map[string]int{"alpha": 10, "beta": 20}14 value, ok := LookupScore(scoresmap[string]int{"alpha":10, "beta":20}, key"beta")value ← 20, ok ← true
5// Generic shape: Lookup[K comparable, V any](items map[K]V, key K) (V, bool)6func LookupScore(itemsmap[string]int{"alpha":10, "beta":20} map[string]int, key"beta" string) (int, bool) {7 value→ 20, ok→ true := items[key]208 return value20, oktrue9}value ← 20, ok ← true
13 scores := map[string]int{"alpha": 10, "beta": 20}14 value→ 20, ok→ true := LookupScore(scoresmap[string]int{"alpha":10, "beta":20}, key"beta")1516 fmt.Println("key=", key"beta")17 fmt.Println("found=", oktrue)18 fmt.Println("value=", value20)19}outputkey= beta found= true value= 20
key ← "alpha", scores ← map[string]int{"alpha":10, "beta":20}
11func main() {12 var key→ "alpha" = "alpha"13 scores→ map[string]int{"alpha":10, "beta":20} := map[string]int{"alpha": 10, "beta": 20}14 value, ok := LookupScore(scoresmap[string]int{"alpha":10, "beta":20}, key"alpha")value ← 10, ok ← true
5// Generic shape: Lookup[K comparable, V any](items map[K]V, key K) (V, bool)6func LookupScore(itemsmap[string]int{"alpha":10, "beta":20} map[string]int, key"alpha" string) (int, bool) {7 value→ 10, ok→ true := items[key]108 return value10, oktrue9}value ← 10, ok ← true
13 scores := map[string]int{"alpha": 10, "beta": 20}14 value→ 10, ok→ true := LookupScore(scoresmap[string]int{"alpha":10, "beta":20}, key"alpha")1516 fmt.Println("key=", key"alpha")17 fmt.Println("found=", oktrue)18 fmt.Println("value=", value10)19}outputkey= alpha found= true value= 10
key ← "delta", scores ← map[string]int{"alpha":10, "beta":20}
11func main() {12 var key→ "delta" = "delta"13 scores→ map[string]int{"alpha":10, "beta":20} := map[string]int{"alpha": 10, "beta": 20}14 value, ok := LookupScore(scoresmap[string]int{"alpha":10, "beta":20}, key"delta")value ← 0, ok ← false
5// Generic shape: Lookup[K comparable, V any](items map[K]V, key K) (V, bool)6func LookupScore(itemsmap[string]int{"alpha":10, "beta":20} map[string]int, key"delta" string) (int, bool) {7 value→ 0, ok→ false := items[key]08 return value0, okfalse9}value ← 0, ok ← false
13 scores := map[string]int{"alpha": 10, "beta": 20}14 value→ 0, ok→ false := LookupScore(scoresmap[string]int{"alpha":10, "beta":20}, key"delta")1516 fmt.Println("key=", key"delta")17 fmt.Println("found=", okfalse)18 fmt.Println("value=", value0)19}outputkey= delta found= false value= 0