Data Bundles (Slice, Map)
Go also has bundles for managing data together.
1. The line of boxes: slice []
This is the most common list-like structure.
shoppingList := []string{"apple", "banana"}
// Add (append)
shoppingList = append(shoppingList, "grape")
// Update (index)
shoppingList[0] = "strawberry"
fmt.Println(shoppingList)
Translation:
Create a string bundle called shoppingList
Add "grape" to the bundle and store it back
Change box 0 to "strawberry"
2. The labeled dictionary: map map
This is a dictionary-like structure where you look up values by key.
myInfo := map[string]string{
"name": "Alex",
"city": "Seoul",
}
// Add & update
myInfo["hobby"] = "Go coding"
// Delete
delete(myInfo, "city")
fmt.Println(myInfo)
Translation:
Create a labeled bundle called myInfo
Put "Go coding" under the "hobby" label
Delete the "city" label
In Go, it is important to clearly state the bundle type in advance (like []string or map[string]string).