Go code
type Counter struct { Value int }
func increment(c *Counter) {
c.Value++
}
c := Counter{}
increment(&c)
fmt.Println(c.Value)Line-by-line explanation
- Counter has an integer field.
- The parameter type *Counter means pointer to Counter.
- The function changes the original field.
- The call passes c’s address.
- The changed value is displayed.
Expected output
1This is the expected result.
Go automatically allows c.Value instead of requiring (*c).Value for struct pointers.