Home  >  Article  >  Backend Development  >  Scope and life cycle of variables in Golang function parameter passing

Scope and life cycle of variables in Golang function parameter passing

PHPz
PHPzOriginal
2024-04-14 08:06:02411browse

Go function parameters only receive copies when passed by value, so modifications within the function will not affect the original variables. By using pointers, pass-by-reference can be achieved so that modifications within a function affect the original variable.

Scope and life cycle of variables in Golang function parameter passing

Variable scope and life cycle in Go function parameter passing

In Go language, variable scope of function parameters Similar to life cycle and local variables. That is, the lifetime of parameter variables is limited to the duration of function execution.

Pass by value

By default, parameters of Go functions are passed by value. This means that the function receives a copy of the argument variable, rather than a reference to the original variable. Therefore, any modifications made to parameter variables within the function will not affect the original variables outside the function.

func changeValue(num int) {
  num = 10
}

func main() {
  num := 5
  changeValue(num)
  fmt.Println(num) // 输出:5
}

Passing by reference

Passing by reference can be achieved by using pointers. When you pass a pointer to a function, the function gets a reference to the original variable, not a copy. This means that modifications to parameter variables within the function will also affect the original variables outside the function.

func changeValue(num *int) {
  *num = 10
}

func main() {
  num := 5
  changeValue(&num)
  fmt.Println(num) // 输出:10
}

Practical case

Let us consider a calculation formula using the volume of a sphereV = (4/3) * π * r³ Calculate the volume of a sphere The program:

import (
  "fmt"
  "math"
)

func calculateVolume(radius float64) float64 {
  return (4 / 3.) * math.Pi * math.Pow(radius, 3)
}

func main() {
  var radius float64
  fmt.Printf("Enter the radius of the sphere: ")
  fmt.Scan(&radius)
  volume := calculateVolume(radius)
  fmt.Printf("The volume of the sphere is: %.2f\n", volume)
}

In this example, the value of the radius parameter is passed to the calculateVolume function via pass-by-value. Inside the function, a copy of the radius argument is used to calculate the volume. Therefore, any modifications to the radius parameter within the function will not affect the original radius variable in the main function.

The above is the detailed content of Scope and life cycle of variables in Golang function parameter passing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn