Pointers and Passby value/reference in GOlang

This is the fourth article in the “Getting Started with GOlang” series. If you have not checked out the previous article please check it out before diving into this. 1. Getting started with GOlang 2. Slices, Custom Types and Receiver Functions(methods) in GOlang 3. Structs and Maps in GOlang 4. Pointers and Passby value/reference in GOlang 5. Interfaces in GOlang
Before understanding pointers, let's see an example using structs in GO. If you do not have a clear understanding regarding structs in go, please visit my previous article about it.
Create a new directory and inside it create a main.go file. Paste the following code inside it.
package main
import "fmt"
type student struct {
firstName string
lastName string
}
func main() {
Peter := student{firstName: "Peter", lastName: "Parker"}
Peter.changeFirstName("Peterson")
Peter.print()
}
func (s student) print() {
fmt.Println("First Name: ", s.firstName, " Last Name: ", s.lastName)
}
func (s student) changeFirstName(f string) {
s.firstName = f
}Now, this is very similar to the struct example in the previous article. The only difference is that there is another receiver function named changeFirstName for the type student. It also takes another string as an argument and replaces the firstName with the provided string.
So basically, we replaced “Peter” with “Peterson” right? The answer is both YES and NO. Now run the program with go run main.go and see the output. The firstName is still “Peter”, which is the evidence for NO.
Now, remove the following line
Peter.print()And add the following line inside the changeFirstName function, at the end.
s.print()Save and run the file again. Now you see the firstName was “Peterson”. That is the evidence for YES. So, what exactly is happening here.
What happens is, when we pass a value inside a function, then a new instance of the value is created inside the function, which is a copy of the value. This holds true also for the receiver functions.
So, s inside the function changeFirstName is a copy of Peter but is not Peter. So, when we change the value of s, its value changes but the original value Peter is unaffected.
Now, replace all the code from main.go by:
package mainimport "fmt"type student struct {
firstName string
lastName string
}func main() {
Peter := student{firstName: "Peter", lastName: "Parker"}PeterPointer := &PeterPeterPointer.changeFirstName("Peterson")Peter.print()
}
func (s student) print() {
fmt.Println("First Name: ", s.firstName, " Last Name: ", s.lastName)
}
func (sp *student) changeFirstName(f string) {
(*sp).firstName = f
}There are only a few changes made. Let's go to the line: PeterPointer := &Peter What does this &Peter mean, even more so, what does & mean? To understand this, we need to understand the meaning of Pointer. Things get a little intense here. Read it thoroughly, we shall go into small details to make things clear.
POINTERS
Peter is a variable that stores some kind of value (in our case a struct). This could be said as Peter is a representation of a value stored in a certain address/location in the memory of the computer. So, when we print Peter, we see the value stored in that memory address.

When Peter is passed into our function, a new variable s is created with identical values, but it will be stored in another memory location, with a different address. And when we change the value of s, it changes the value stored in it’s location but the value stored in the location represented by Peter is unaffected. That is why, Peter remained unchanged. This is what we call passby Value.

Now, by adding & in front of a variable it returns the memory address of the variable. So, when what mean by the line PeterPointer := &Peter is that we are assigning the memory address of variable Peter to PeterPointer (which is also another variable). These kinds of variable that contain the memory address of another variable are called pointers. Cause what it is essentially doing is, pointing towards the memory address that has the actual value.

And you can also see the line with (*sp). What * does is, if you put it in front of a memory address (pointer), it gives you access to the values present in the memory address ( that the pointer contains). So, if we write *PeterPointer , then it would represent the values inside the memory address that PeterPointer holds, which is Peter. so Basically, (*PeterPointer == Peter ) is true.
Now, Let's look at the line :
PeterPointer.changeFirstName("Peterson")And function:
func (sp *student) changeFirstName(f string) {
(*sp).firstName = f
}Here, PeterPointer is not a struct of type student, but a pointer to a struct type student so if we want to use the receiver function for a pointer, the receiver type must be changed. Hence (sp *student) is used instead of (s student) that we previously used. And in the case of types in GO, * is added in front of a type to denote that, we are actually passing a pointer of that type, not the actual variable.
Now, stay with me here, as I said, when we pass a variable to a function, then a new variable is created with identical values but in a different memory location. So, s in the function changeFirstName, is also a new variable but it contains the same values as by PeterPointer, which is the memory address of the variable Peter.

So, as *PeterPointer, represents the value inside the memory address it contains, which is Peter. Similarly, *sp, also represents the same value inside Peter, as it also contains the same memory address. So, basically *sp == Peter is true And finally, when we make the changes to (*sp).firstName, the actual values of the variable Peter is changed. And this is what we called, passby reference because instead of the actual values, a reference address is passed which is then used to make changes to the actual value. Now, run the program, you should see that the values inside the struct Peter actually changes as per our need.
Now, there is one simple trick in going to make our life easier. Go ahead and delete the line:
PeterPointer := &PeterAnd change the line following it to:
Peter.changeFirstName("Peterson")But, Do not make any changes to the function. Try running the program, you will see the same results.
This is because, in GO, if we want to take the address of the variable we are using, then we do not need to specify it at the calling end. Specifying the nature of the passing (value or reference) at the function is sufficient, it automatically uses the address of the variable in use.
I hope this makes things regarding Pointer and Passby value/reference in a function clear. If there are any questions feel free to leave a response. Stay tuned for more articles regarding GO. Happy Coding :)





