Created
May 13, 2023 07:45
-
-
Save dishbreak/f5446908487682979c33b889078b045e to your computer and use it in GitHub Desktop.
Using a map of slices without initialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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