2

http://play.golang.org/p/wYgfLLQYdm

See example above. In my example the variable change does not last after the method is executed. How can I change a struct variable's value with a struct method?

1 Answer 1

6

Your method receiver is a value, not a pointer.

That means those methods like switch_width_height() operate on a copy of the object.
See also:

Add a '*':

func (s *square) switch_width_height()

And it will work as expected: see play.golang.org.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.