SoFunction
Updated on 2025-03-05

Is the parameter passing in go value passing or reference passing implementation

In Go language, the parameter transfer mechanism is an important concept, which determines whether the modification of parameters inside the function will affect the original data. Regarding the question of whether parameter passing in Go is value passing or reference passing, we can answer it from the following aspects.

1. Definition of value transfer and reference transfer

  • Value pass: In value transfer, when the function is called, the value of the real parameter will be copied and the copy will be passed to the corresponding formal parameter. Operations on formal parameters inside the function will not change the original value of the actual parameter.
  • Reference pass: In reference pass, the memory address of the actual parameter is passed, not the actual value. Therefore, any modification of formal parameters inside the function will directly affect the value of the original actual parameter.

2. Parameter transfer mechanism in Go language

  • Default is value passing: In Go language, all function parameter passing is value passing by default. This means that when passing the parameter to the function, it is actually passing the copy of the parameter to the function, and the modification of the copy inside the function will not affect the original data.
  • Special handling of reference types: Although Go uses value passing by default, the situation varies for some reference types (such as slices, maps, channels, interfaces, and pointers). When these types are passed as parameters to a function, although the value is passed, the value itself is a reference. Therefore, modifying these types of parameters inside the function may affect the original data.

3. Example description

  • Value passing of basic types
func modifyValue(x int) {
    x = 100
}

func main() {
    original := 1
    modifyValue(original)
    (original) // Output 1, not modified}

In this example,originalIt's aintVariable of type, when passed tomodifyValueWhen a function is passed, it is a copy of it. Therefore, inside the functionxThe modification will not affectoriginalvalue.

  • "Reference Pass" effect of slices
func modifySlice(s []int) {
    s[0] = 100
}

func main() {
    originalSlice := []int{1, 2, 3}
    modifySlice(originalSlice)
    (originalSlice) // Output [100, 2, 3], the first element is modified}

althoughoriginalSlicePassed as a value tomodifySlicefunction, but this value is actually a reference to a slice. The slice contains a pointer to the array, so modifying the slice's elements inside the function actually modifies the internal array, thereby affecting the original slice.

  • Pointer achieves reference transfer effect

For basic types, if you want to modify their values ​​inside a function, you can use pointers to achieve a reference pass effect similar to that of reference pass.

func modifyPointer(x *int) {
    *x = 100
}

func main() {
    original := 1
    modifyPointer(&original)
    (original) // Output 100, modified}

In this example, theoriginalThe address of the variable is givenmodifyPointerFunction. Because it is passed a copy of the pointer to the original data, when the data is modified through this pointer inside the function, the value of the original variable is actually modified.

4. Summary

Parameter passing in Go is value passing by default, which means that a copy of the parameters is passed. However, for reference types (such as slices, maps, channels, interfaces, and pointers), although values ​​are passed, the value itself is a reference, so modifying these types of parameters inside the function may affect the original data. Understanding this is essential for writing correct and efficient Go code.

This is the article about whether the parameter passing in go is value passing or reference passing implementation. For more related go parameter passing content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!