Sunday, June 4, 2017

How does pass by value work in Go, when you pass pointers?

So have you also read that everything is passed by value in Go and thought then how come if a pointer is passed, the value pointed to by that pointer also changes?

Well this is how it works. Comments welcome!



Note the function change() returns the contents of the copied pointer only and the content is basically the address that its pointing to! Its pointing to the address of variable 'i'. So here we have two pointers pointing to the same variable with address (0x1040a124) but those two pointers have different addresses themselves (0x1040c118 and 0x1040c120).

No comments:

Post a Comment