×

Go Tutorial

Go Basics

Go Variables

Go Literals

Go Type Handling

Go Operators

Go Decision Making

Go Loops

Go Functions

Go String

Go Arrays

Go Slices

Go Maps

Go Pointers

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Go Call by Value

Last Updated : July 19, 2025

In Go, when you pass a variable to a function, the language uses Call by Value by default. This means the function gets a copy of the original variable. Any changes made to the parameter inside the function do not affect the original variable outside the function.

Understanding Call by Value

When a function is called, the values of the actual parameters (arguments) are copied into the function's formal parameters. Go does not pass the reference or address of the original variable unless explicitly done using pointers.

Example: Call by Value in Go

The following example demonstrates pass-by-value in Go, where changes made to a function parameter do not affect the original variable:

package main
import "fmt"

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

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

When you run the above code, the output will be:

Inside function, num = 11
Outside function, value = 10

Explanation

In the above example, value is passed to the increment() function. Inside the function, only a copy of value is modified. The original variable remains unchanged outside the function.

Why Use Call by Value?

  • Ensures that the original data is safe and not accidentally modified.
  • Makes functions easier to reason about and debug.
  • Good for small data types like int, float, and bool.

Call by Value with Structs

When you pass a struct to a function in Go, it is also passed by value. This means a copy of the struct is passed, and changes inside the function do not affect the original struct.

Example: Passing Struct by Value

The following example illustrates that Go passes structs by value, meaning changes made to the struct inside a function do not affect the original struct outside:

package main
import "fmt"

type Student struct {
    name string
    age  int
}

func updateAge(s Student) {
    s.age = s.age + 1
    fmt.Println("Inside function, updated age =", s.age)
}

func main() {
    stu := Student{name: "Rahul", age: 20}
    updateAge(stu)
    fmt.Println("Outside function, original age =", stu.age)
}

When you run the above code, the output will be:

Inside function, updated age = 21
Outside function, original age = 20

Explanation

In this example, the Student struct is passed to the updateAge() function. Inside the function, the age is incremented, but it does not affect the original struct in main() because the function works on a copy of the struct.

Exercise

Test your understanding of Call by Value in Go:

  1. What happens when you modify a parameter inside a function in Call by Value?
  2. Which of the following is true about Call by Value?
  3. What is the default parameter passing technique in Go?

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.