Why isn't the slice being modified in the following code:
package pointers
import "fmt"
func modifyObject(v *Vertex) {
v.x = v.x * v.x
v.y = v.y * v.y
}
func modifyArray(vertices *[]Vertex) {
for _, v := range *vertices {
v.x = v.x * v.x
v.y = v.y * v.y
}
}
func DemoPointersArray() {
v := Vertex{2, 3}
modifyObject(&v)
fmt.Println("Vertex modified successfully:", v)
v1 := Vertex{2, 3}
v2 := Vertex{20, 30}
vertices := []Vertex{v1, v2}
modifyArray(&vertices)
fmt.Println("Vertices are NOT modified:", vertices)
}
Output:
Vertex modified successfully: {4 9}
Vertices are NOT modified: [{2 3} {20 30}]
How to modify them?