Suppose we have
type Circle struct { x float64 y float64 r float64 }
We can access fields using the . operator:
fmt.Println(c.x, c.y, c.r) c.x = 10 c.y = 5
The following function circleArea
function uses a Circle:
func circleArea(c Circle) float64 { return math.Pi * c.r*c.r }
In main, we have:
c := Circle{0, 0, 5} fmt.Println(circleArea(c))
Arguments are always copied in Go.
To modify one of the fields inside of the circleArea
function, it would not modify the original variable.
Typically we write the function using a pointer to the Circle:
func circleArea(c *Circle) float64 { return math.Pi * c.r*c.r }
And change main to use & before c:
c := Circle{0, 0, 5} fmt.Println(circleArea(&c))