Arrays, Slices and Maps
Arrays have a fixed size. Slices are flexible views you grow with append. Maps hold key value pairs and range walks over both. len gives the size.
Example
nums := []int{1, 2, 3}
nums = append(nums, 4)
fmt.Println(nums, len(nums), nums[1:3])
ages := map[string]int{"Ada": 21, "Tunde": 30}
ages["Zainab"] = 25
fmt.Println(ages["Ada"], len(ages))
for i, n := range nums {
fmt.Println(i, n)
}
if _, ok := ages["Bob"]; !ok {
fmt.Println("Bob missing")
}Result
[1 2 3 4] 4 [2 3] 21 3 0 1 1 2 2 3 3 4 Bob missing
Exercise
Grow a slice.
nums = (nums, 4)
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.