Go – Function Basics
Estimated reading: 3 minutes 36 views

📤 Go Call by Value – Understand How Go Passes Function Arguments (2025 Guide)

🧲 Introduction – What Is Call by Value in Go?

In Go, all function arguments are passed by value by default. This means the function receives a copy of the variable, not the original. So any modification to the parameter does not affect the original variable outside the function.

🎯 In this section, you’ll learn:

  • What “call by value” means in Go
  • How Go handles function argument passing
  • Examples with primitives and structs
  • How to modify values using pointers instead

✅ What Is Call by Value?

When you pass a variable to a function:

  • Go copies the value into the parameter
  • Changes inside the function do not affect the original variable

✏️ Example – Call by Value with Integers

func increment(x int) {
    x = x + 1
    fmt.Println("Inside function:", x)
}

func main() {
    num := 5
    increment(num)
    fmt.Println("Outside function:", num)
}

📤 Output:

Inside function: 6  
Outside function: 5

num remains unchanged because only a copy was passed to increment().


🧱 Example – Call by Value with Structs

Even complex types like structs are passed by value unless you use pointers.

type Person struct {
    name string
}

func changeName(p Person) {
    p.name = "Bob"
}

func main() {
    person := Person{name: "Alice"}
    changeName(person)
    fmt.Println("Outside function:", person.name)
}

📤 Output:

Outside function: Alice

✅ The name change doesn’t persist because p is a copy of person.


🔁 Want to Modify the Original Value?

Use pointers to achieve “call by reference” behavior.

func increment(x *int) {
    *x = *x + 1
}

func main() {
    num := 5
    increment(&num)
    fmt.Println("Outside function:", num)
}

📤 Output:

Outside function: 6

✅ The function modifies the original value because it received a memory reference.


⚖️ Comparison Table – Call by Value vs Pointer

FeatureCall by ValueUsing Pointers
Value Modified❌ No✅ Yes
Memory Efficiency✅ Safer⚠️ Depends on object size
Side Effects❌ Avoided✅ Possible
Use CasesSimple reads, safetyUpdates, in-place changes

🔄 Call by Value Is Still Useful

Use it when:

  • You only read values inside the function
  • You want to avoid accidental mutations
  • You’re working with small/simple data types

📌 Summary – Recap & Next Steps

Go uses call by value for all function arguments, including arrays, structs, and primitive types. If you need to modify the original data, use pointers instead.

🔍 Key Takeaways:

  • Go passes function arguments by value
  • The original variable is not modified
  • Use pointers to change values across function calls
  • Avoid side effects by sticking to value passing when safe

⚙️ Next: Learn about Go Call by Reference using pointers for controlled mutation.


❓ FAQs – Go Call by Value

❓ Does Go support call by reference?
✅ Not directly. But you can simulate it using pointers.

❓ Are structs also passed by value in Go?
✅ Yes. Unless you use a pointer, the whole struct is copied.

❓ Can I modify a variable inside a function?
✅ Only if you pass a pointer to it.

❓ Is call by value memory-safe?
✅ Yes. It prevents unintended changes to original variables.

❓ How do I know if a value is passed by reference?
✅ Look for pointer syntax: *Type in parameters and &variable in arguments.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Go Call by Value

Or Copy Link

CONTENTS
Scroll to Top