Session 17 — Pointers, references, dereferencing and more — Golang

Stephens Xu
Fullstack Network
Published in
3 min readJun 8, 2017

--

In this session we continued to refactor Gong into CLI tool that can potentially be used for different project management software.

Gong was initially built to be a CLI tool for Jira and github integration, now we want to make it more generic so it can interact with other API such as Trell or PivotalTracker.

As we progressed, I became increasingly confused and frustrated with one of the most common struggles with most Go beginners: difference between passing by pointer or passing by value.

All of my time as an engineer I spent with Ruby, so coming to go, I not only had to deal with statically typed programs but also the concept of pointers, references and values.

In theory, I do understand the difference. Pointers are not values, they are actually reference to an address in the memory, which stores the actual value we want. But in practice of Go, it took quit a bit of effort for Avi to explain this to me.

play.golang is a good place to run simple go code for experiment purpose, I’ll be using some screen shot to illustrate the differences.

First example is pass by value, consider the following code:

package mainimport (
"fmt"
)
type Sample struct {
Name string
}
func SetName(s Sample) {
s.Name = "foo"
fmt.Println("this is inside SetName")
fmt.Println(s.Name)
}
func main() {
s := Sample{}
s.Name = "bar"
fmt.Println(s.Name)
SetName(s)
fmt.Println("this is outside SetName")
fmt.Println(s.Name)
}

What would you expect the outcome to be? Here is what actually got printed:

Here, after setting s.Name = "bar", we can observe that both before and after calling SetName, s.Name returns "bar", it doesn’t modify our original s.Name at all. The only time s.Name got printed as "foo" is inside of the function SetName. This is because here the s inside of SetName lives in a different memory address than the s outside of SetName, therefore any attempted modification doesn’t effect our original s.

Now for the second example where we pass by pointer:

package mainimport (
"fmt"
)
type Sample struct {
Name string
}
func SetName(s *Sample) {
s.Name = "foo"
fmt.Println("this is inside SetName")
fmt.Println(s.Name)
}
func main() {
s := &Sample{}
s.Name = "bar"
fmt.Println(s.Name)
SetName(s)
fmt.Println("this is outside SetName")
fmt.Println(s.Name)
}

Here is what got printed:

Notice the difference in result than example one. Here our s variable is a pointer. When we pass the pointer around, we are able to modify the same variable s both inside and outside of the SetName function. As observed, calling SetName on our s variable changed our original s.Name from "foo" to "bar" .

Hope this helps a bit with anyone that’s new go Golang and get frustrated with pointers. Last night’s streaming we had great participation from our viewers. Thank you KenBurnsJr for providing your insights and referencing to this great article.

We’ll be streaming again tonight, join us if this interest you!

--

--