Pass by Value and Pass by Reference in Go

Learn the difference so you don’t make costly mistakes

Adeshina H. Hassan
Mar 15 · 4 min read
Image for post
Image for post
Photo by Nick Fewings on Unsplash

Introduction

What is Pass-By-Value?

If you run through the examples above, you can confirm that the values of variables passed to the functions remain the same before and after the functions calls. In a nutshell, the variables were passed by value.

Now, let’s explore the other way Go function treats parameters — pass-by-reference.

What is Pass-By-Reference?

Before we move on to some examples of how Go function treats the Composite types (Slice, and Map), Channel, Pointer, and function, let us take a look at this snippet of code that confirms Go composite types are not Passed-By-Reference:

package mainimport "fmt"
func myMap(v map[int]int) {
v = make(map[int]int) // make() declares and initializes v to 0
}
func myInt(v []int) {
v = make([]int) // make() declares and initializes v to 0
}func main() {

//v is declared but NOT initialized, which means its value is nil
var v map[int]int
myMap(v)
fmt.Println(v == nil) // true
//i is declared but NOT initialized, which means its value is nil
var i []int
myInt(i)
fmt.Println(i == nil) // true
}

Looking at the example above, we could tell that even after declaring a variable “V”, then calling myF() on it in order to initialize it to 0. Eventually, when we test for its value after the call, it results in “true". This means myF() did not treat “v” as Pass-By-Reference (because Go does not support that semantic). The same result we would get if we try Slice and Channel. You can read more on this topic in this GREAT Dave Cheney’s blog.

Below are the example of passing the Composite types and others (asides from Primitive types discussed above) in Go:

Running through the examples above, we can see the effect of passing parameters to functions. In the slice, for example, we can confirm that the value of the variable coffeeBox was modified when passed to the function modifySlice. It’s the same with map, pointer, function, and channel.

If you find yourself in need of modifying the value of a basic type (int, float, bool, etc), simply pass the variable’s memory address to the function (in other words, treat the parameter as a pointer). The pointer section clearly exemplifies this scenario.

Summary

In this short article, we’ve explored the two ways that Go treats parameters passed to its function. It’s important to be aware of this concept so as to avoid false/wrong expectations.

Until the next article, keep Go-ing!

Better Programming

Advice for programmers.

Adeshina H. Hassan

Written by

Bilingual || Software Engineer || co-organizer @GolangNigeria #golang #flutter Twitter Handle: @adeshinaHH Personal Website: https://adeshinahassan.com

Better Programming

Advice for programmers.

Adeshina H. Hassan

Written by

Bilingual || Software Engineer || co-organizer @GolangNigeria #golang #flutter Twitter Handle: @adeshinaHH Personal Website: https://adeshinahassan.com

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store