How To Do Call By Value Vs Call By Reference In Go Lang.
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
Search for a command to run...
Learner, Love to make things simple, Full Stack Developer, StackOverflower, Passionate about using machine learning, deep learning and AI
No comments yet. Be the first to comment.
Move beyond traditional RESTful thinking. Learn how to design APIs specifically for MCP (Model Context Protocol) servers. This guide covers the shift in mindset, a practical OpenAPI 3.1 example, and a Spring Boot implementation to make your services ...

Extending Kestra to Every Corner of Your Data Stack. Introduction: The Power of Plugins Imagine you're a master chef. You don't just have one knife - you have specialized tools for every task: a paring knife for delicate work, a chef's knife for chop...
Mastering Complex Orchestration Scenarios. Introduction: The Orchestrator's Toolkit Imagine you're conducting a symphony. You don't just wave your baton - you cue sections, adjust tempo, handle surprises, and ensure harmony. That's what advanced work...
From Data Extraction to Loading - A Practical Guide Introduction: Why ETL Still Matters in the Modern Data Stack Remember when data engineering was "extract, transform, load"? Some say ETL is dead, replaced by ELT, reverse ETL, and data mesh. But her...
Building Blocks of Declarative Orchestration. Introduction: The Power of Simplicity Imagine trying to build a house without understanding bricks, beams, and blueprints. That's what using an orchestration tool without understanding its core concepts f...
In Go, there are two ways to pass arguments to a function: call by value and call by reference. In call by value, the value of the argument is copied to a new variable and passed to the function, while in the call by reference, the memory address of the argument is passed to the function. In this article, we will discuss the differences between these two methods with examples.

When a function is called using call by value, a new copy of the variable is created and passed to the function. The function can modify the value of the copy, but the original value remains unchanged.
Let's take an example to understand call by value:
package main
import "fmt"
func swap(x, y int) {
var temp int
temp = x
x = y
y = temp
}
func main() {
a, b := 5, 10
fmt.Println("Before swapping, a=", a, "b=", b)
swap(a, b)
fmt.Println("After swapping, a=", a, "b=", b)
}
Output:
Before swapping, a= 5 b= 10
After swapping, a= 5 b= 10
In the above example, the swap function takes two arguments x and y. Inside the function, a temporary variable is used to swap the values of x and y. However, when we call the swap function with a and b, the values of a and b remain unchanged because the function is called using call by value.
When a function is called using call by reference, a pointer to the variable is passed to the function. The function can then access and modify the value of the variable using the pointer.
Let's take an example to understand call by reference:
package main
import "fmt"
func swap(x, y *int) {
var temp int
temp = *x
*x = *y
*y = temp
}
func main() {
a, b := 5, 10
fmt.Println("Before swapping, a=", a, "b=", b)
swap(&a, &b)
fmt.Println("After swapping, a=", a, "b=", b)
}
Output:
Before swapping, a= 5 b= 10
After swapping, a= 10 b= 5
In the above example, the swap function takes two pointers x and y. Inside the function, a temporary variable is used to swap the values of *x and *y. When we call the swap function with the address of a and b, the values of a and b are swapped because the function is called using call by reference.
In Go, call by value and call by reference are two ways to pass arguments to a function. Call by value creates a new copy of the variable and passes it to the function, while call by reference passes a pointer to the variable to the function. It is important to choose the appropriate method based on the requirements of the program.