
Passing variables in Go can be a bit tricky, especially when it comes to understanding the difference between pass by value and reference.
In Go, all variables are passed by value by default. This means that when you pass a variable to a function, a copy of the original value is created and passed to the function.
Passing by value is useful when you don't want the original variable to be modified. For example, if you pass a string to a function and the function tries to modify it, the original string will remain unchanged.
A classic example of passing by value is when you pass a numeric value to a function, like this: `func add(x int) { x = x + 1 }`. If you call this function with the value 5, the function will modify the copy of the value, but the original value 5 will remain unchanged.
You might enjoy: Pass Html Element to Pass through Primevue
Basic Concepts
Passing variables into methods in Go can be done in two ways: pass by value and pass by reference. Pass by value copies the value of the variable into another memory location and passes the newly created one into the method, so anything that happens to the variable inside the method will not affect the original variable.
Pass by reference, on the other hand, passes the memory location instead of the value, so anything that happens to the variable inside the method will affect the original variable.
Here's a quick comparison of the two:
- Pass by value: Copies the value, no impact on original variable
- Pass by reference: Passes memory location, impact on original variable
Basic of Pointer
In programming languages like Go, understanding how to work with pointers is crucial. Pointers are variables that store memory addresses as their values.
Pass by value and pass by reference are two important concepts to grasp when working with programming languages that support pointers. This includes languages like Java, C#, C/C++, and Go.
Pass by value passes the value of a variable into a method, creating a copy of the original variable in another memory location. This means anything that happens to the variable inside the method will not affect the original variable.
Pass by reference, on the other hand, passes the memory location of the variable instead of its value. This means anything that happens to the variable inside the method will affect the original variable.
Here's a summary of the two concepts:
- Pass by value: Passes the value of the variable into the method, creating a copy in another memory location.
- Pass by reference: Passes the memory location of the variable instead of its value.
Basic Data Type
In Go, basic data types are the foundation of programming. They include int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, string, bool, byte, and rune.
Go copies the value of a variable when passed to a function, resulting in a new memory location. This means that changing the value within the function does not affect the original variable.
The Add method in the first example demonstrates this, where incrementing x does not change the value of a.
Go also has a pass-by-value mechanism, which means that variables passed to a function are copied, not referenced.
Other basic data types, such as Array and Struct, have the same property as the basic data types.
Here's a list of basic data types in Go:
- int
- int8
- int16
- int32
- int64
- uint
- uint8
- uint16
- uint32
- uint64
- uintptr
- float32
- float64
- string
- bool
- byte
- rune
- Array
- Struct
Passing Values
In Go, passing values is a fundamental concept that's crucial to understand. We have access to deal with a pointer, which means we must understand how passing values works.
If this caught your attention, see: How to Store Values in Interface Golang
Passing values in Go is done by value, not by reference. This means that when you pass a value to a function, a copy of the original value is created.
Go's pass by value approach is different from other languages that pass by reference. This difference can lead to unexpected behavior if you're not careful.
When you pass a value to a function, you can't modify the original value. If you try to modify it, you'll only be modifying the copy that was created when the function was called.
This is because Go's pass by value approach creates a new copy of the original value when it's passed to a function. Any changes made to the copy won't affect the original value.
A different take: Golang Go
Functions
Functions in Go can be declared with a struct as the prefix, known as a struct function. This can be done using the func (s struct) FuncName() or func (s *struct) FuncName() syntax.
You might enjoy: Golang Struct
Declaring a struct function with a pointer, such as func (s *struct) FuncName(), allows you to modify the original struct. The function will pass a reference to the original struct, allowing changes to be reflected outside the function.
A struct function declared without a pointer, like func (s struct) FuncName(), creates a copy of the struct and passes it to the function. This means any changes made to the struct within the function will not affect the original struct.
Pass-By-Value
In Go programming language, primitive types like int, float, and bool are passed by value.
All primitive types in Go are passed by value, meaning a copy of the variable is made and passed to the function.
Passing by value is usually how your values are passed on to functions.
If you run through the examples, you can confirm that the values of variables passed to the functions remain the same before and after the functions calls.
See what others are reading: Golang Data Types
In a nutshell, the variables were passed by value.
Go does not support pass-by-reference for primitive types.
The original value is never modified when accessing or modifying the variable within your function.
If you find yourself in need of modifying the value of a basic type, simply pass the variable’s memory address to the function (in other words, treat the parameter as a pointer).
Modifying Original
You can modify the original slice by passing a pointer to a slice to a function. This works because a reference to the original slice header is passed, so when you append to the slice in the function, you're modifying the original slice header, and the change is reflected in the original.
Passing a pointer to a struct to a function also allows you to modify the original struct. The pointer still points to the same original data, even though the function receives a copy of the pointer.
If this caught your attention, see: Golang Make Slice
In Go, structs are passed by value to a function, which means that only a copy of the struct is passed. This is important to note, especially if your struct contains references to other data types like maps or channels.
To modify the original struct, you need to pass a pointer to the struct to the function. This way, the function can modify the original data.
Here's a comparison of passing by value and passing by reference in Go:
So, if you want to modify the original slice or struct, you should pass a pointer to it to the function. If you pass the struct itself, you'll only be working with a copy, and any changes you make will be lost when the function returns.
Slices and Structs
Slices are a fundamental concept in Go, and understanding how they work is crucial when it comes to passing them around in your code. Slices are essentially references to an underlying array.
A slice has three key fields: a pointer to the underlying memory, a "size" field, and a "capacity" field. The actual value contents of the slice are stored in the underlying memory.
Copying a slice or passing it as a value doesn't actually copy the underlying array or the data within - it merely copies the pointer, size, and capacity. This means manipulating the boundaries of a slice is extremely cheap.
You can have multiple slice references pointing to the same underlying array in memory, even if those slices are of different sizes. This is because the slice itself serves as a reference to the underlying space.
Passing a slice as a value is not the same as passing the underlying array or its contents. The thing being passed-by-value is the slice reference, not the contents of the slice.
Structs, on the other hand, are value types and don't contain built-in references like slices do. When you pass a struct, its value is copied, including all the value types within it.
However, if a struct contains a slice, map, or channel reference, only the references themselves are copied. This means passing a struct as a value that contains a map, channel, or slice is not enough to guarantee that the receiving function can't modify the contents of the original struct beyond its own scope.
Intriguing read: Golang Chan
Takeaways
Passing by value in Go can be tricky, especially with more complex types. Go is still passing by value in the truest sense of the term.
The references are being passed as values instead of the contents, which can lead to unintuitive behavior. This can result in difficult-to-trace errors and bugs within a program.
Even built-in functions like append can lead you astray if you make incorrect assumptions about whether or not a copy will take place. You should always be on the lookout for this.
To guarantee a true copy is passed, it's best to copy the maps or slices yourself. This will help prevent any confusion or unexpected behavior.
Expand your knowledge: Golang Deep Copy
Frequently Asked Questions
Is Netflix using Golang?
Yes, Netflix is a notable user of Go (Golang), leveraging its capabilities for building high-performance internal tools. Netflix's Chaos Monkey is a notable example of a tool built with Go.
Featured Images: pexels.com


