What can we do when dealing with many structs of the same type? We can use them in an array together! Let’s say we want to create an array of the following points: {1, 1} {7, 27} {12, 7} {9, 25}
We create an array of Point
s like so:
points := []Point{{1, 1}, {7, 27}, {12, 7}, {9, 25}}
If the points have names, we can also create the array like this:
a = {1, 1} b = {7, 27} c = {12, 7} d = {9, 25} points := []Point{a, b, c, d}
We can access the contents of this array like we would for an ordinary array. We can also access and modify the fields of each of the array elements.
points := []Point{{1, 1}, {7, 27}, {12, 7}, {9, 25}} fmt.Println(points[0]) // Output will be {1, 1}
points := []Point{{1, 1}, {7, 27}, {12, 7}, {9, 25}} points[1].x = 8 points[1].y = 16 fmt.Println(points[1]) // Output will be {8, 16}
Arrays of structs allow us access many instances of our structs together in our programs! In the following checkpoints, we will practice working with arrays of structs.
Instructions
Given the struct Cake
which represents a cake, create an array of Cake
s called cakes
that will contain the information for the following cakes:
- type: Chocolate, weight: 1000g
- type: Carrot, weight: 500g
- type Ice Cream, weight 2000g
Print the weight of the chocolate cake.
Change the weight of the carrot cake from 500g to 1500g.