Skip to content

Instantly share code, notes, and snippets.

@dishbreak
Created May 13, 2023 07:45
Show Gist options
  • Save dishbreak/f5446908487682979c33b889078b045e to your computer and use it in GitHub Desktop.
Save dishbreak/f5446908487682979c33b889078b045e to your computer and use it in GitHub Desktop.
Using a map of slices without initialization
package main
import "fmt"
type person struct {
name string
age int
}
func main() {
people := []person{
{"Jim", 7},
{"Janna", 5},
{"Jake", 5},
{"Joseph", 7},
{"Jennifer", 5},
}
histogram := make(map[int][]string)
for _, p := range people {
histogram[p.age] = append(histogram[p.age], p.name)
}
// map[5:[Janna Jake Jennifer] 7:[Jim Joseph]]
fmt.Println(histogram)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment