type Point struct {
x_value int
y_value int
}
func changePointX(pt *Point, new_x_value int) {
pt.x_value = new_x_value <-------------------------------------
}
func updateName(n *string) {
*n = "wedge" <-------------------------------------
}
func main() {
point1 := &Point{1, 2}
fmt.Printf("point 1: %v\n", point1)
changePointX(point1, 100)
fmt.Printf("point 1: %v\n", point1)
name := "tifa"
m := &name
updateName(m)
fmt.Println(name)
}
hello ppl ,
i would like to understand why am i using
*n = "wedge"
when i want to update a value of string , and when i want to update a value of a struct i am using :
pt.x_value = new_x_value
when pt.x is without astrix (*) in the beginning - i think it is also should be like this :
*pt.x_value = new_x_value <— but this is not working and the code up is working.
@NobbZ - so the key to your answer is when i am using struct.field the “.” (dot) will deference pointer as needed , but what comes to mind is when go decides that it is needed or not and based on what ?
@mje - can u plz elaborate why the round brackets will make this work ?