Maps
- Collections of value types that are access via keys - Created via literals or via "make" function - Creation syntax: - name = map[type]type() - Alt creation = make(map[type]type) - Members accessed via [key] syntax and .dot syntax - myMap["key"] = "value" - myMap.value - Check for presence with "value, ok" form of result - Multiple assignments refer to same underlying data
// Example code
func main() {
statePopulations := map[string]int {
"California": 1,
"Texas": 2,
"Florida": 3,
"New York": 4,
"Pennsylvania": 5,
"Illinois": 6,
"Ohio": 7,
}
fmt.Println(statePopulations)
}
// NOTE: The types must be support the ability for equality
Structs
- You can mix-and-match datatypes in structs, unlike in other types like arrays - Naming convention is the same: PascalCase for export, camelCase for import - Keyed by named field - Normally created as types, but anonymous structs are possible - Structs are value types, meaning copying them will make a new struct - No inheritance, but can use composition via embedding - Tags can be added to struct fields to describe fields - If there is no data in a certain field, you can ignore that field in the struct entirely
// Example code
type Doctor struct {
number int
actorName string
companions []string
}
func main() {
aDoctor := Doctor {
number: 3,
actorName: "Jon Pertwee",
companions: []string {
"Liz Shaw",
"Jo Grant",
"Sarah Jane Smith",
},
}
fmt.Println(aDoctor)
}
