Performance and Benchmarking Basics
Map Lookup Shape
Maps make lookup code direct when the program needs to find a value by key.
Map Lookup Shape
map_lookup_shape.go
package main
import "fmt"
func main() {
var key =
scores := map[string]int{
"alpha": 3,
"beta": 5,
"gamma": 8,
}
value, ok := scores[key]
result := "miss"
if ok {
result = "hit"
}
fmt.Println("key=", key)
fmt.Println("value=", value)
fmt.Println("found=", ok)
fmt.Println("result=", result)
}
package main
import "fmt"
func main() {
var key =
scores := map[string]int{
"alpha": 3,
"beta": 5,
"gamma": 8,
}
value, ok := scores[key]
result := "miss"
if ok {
result = "hit"
}
fmt.Println("key=", key)
fmt.Println("value=", value)
fmt.Println("found=", ok)
fmt.Println("result=", result)
}
package main
import "fmt"
func main() {
var key =
scores := map[string]int{
"alpha": 3,
"beta": 5,
"gamma": 8,
}
value, ok := scores[key]
result := "miss"
if ok {
result = "hit"
}
fmt.Println("key=", key)
fmt.Println("value=", value)
fmt.Println("found=", ok)
fmt.Println("result=", result)
}
lookup
Performance decisions often start with choosing a data shape that matches the operation: scan a slice or look up a key.