Examples of go value passing and address passing

Time:2021-1-26

Let’s start with a simple example

1. Add a & in front of the variable to indicate the address of the variable (that is, the pointer of the variable), and add a * in front of the pointer variable to indicate the corresponding value of the pointer

//main.go
package main
func ByAss() {
  s := 100
  P: = & S // the address of S is given to P

  fmt.Println (* P) // 100, so the value of P becomes 100
  *p += 100
  fmt.Println(*p)  //200
  fmt.Println (s) // 200 P has the same address as s, so s becomes 200
}

//demo.go
package main
func main() {
  ByAss()
}

Go passes parameters by value by default, that is, copies of parameters. After the function receives the parameter copy, it may change the value of the copy in the process of using the variable, but it will not affect the original variable, such as function (arg1).

2. Normal value passing (note this example, it seems very simple, but it is easy to ignore)

func ByValue1(n1 int) {
  //At this time, N1 is a copy of N, so the address is different
  fmt.Println(&n1)//0xc00000a098
}

package main
func main() {
  n := 100
  fmt.Println(&n) //0xc00000a090
  Byvalue1 (n) // deliver copy
}

If you want the function to modify the value of the parameter directly, instead of operating on the copy of the parameter, you need to pass the address of the parameter (add the & symbol before the variable name, such as & variable) to the function, which is passed by reference, such as function (& arg1). At this time, a pointer is passed to the function.

3. If you want to use address delivery

func main() {
    n := 100
    fmt.Println(&n) //0xc00000a090
    BYREFERENCE // pass pointer
    fmt.Println(n) //200
}

//Note that the function parameter type needs to be a pointer (*)
func ByReference(n1 *int) {

   *n1 += 100
   fmt.Println The (N1) // 0xc000000a090 address is the same as N, so the value of n is changed
  //Attention
  //IP: = & N1 // there is a problem with this writing method, indicating the address of the address
  //fmt.Println(ip)
}

4. Look at arrays

  • (a) Arrays are also value passing
    arr := [5]int{1,3,5,6,7}
    Arrbyvalue (& ARR) // the & symbol is also required
    fmt.Println(arr) //[100 300 5 6 7]
    func arrByValue(arr1 *[5]int) {
     arr2 := arr1
     arr2[0] = 100
     arr2[1] = 300
    }

5. The following is an example of slicing (variable array). Slicing is passed by reference

func main() {
    arr := []int{1,3,5,6,7}
    BBB (ARR) // no & sign used
    fmt.Print (arr [0]) // 11 the result has changed, indicating that the array is address passing

    //Even if not through a function, it is also passed by reference
    //arr := []int{1,3,5,6,7}
    //arr2 := arr
    //arr2[1] = 0
    //fmt.Print(arr)//[1 0 5 6 7]
}
//aaa
func bbb(arr1 []int) {
   arr1[0] += 10
}
  • (b) When a function is called, reference types such as slice, map, interface and channel all use reference passing by default (even if there is no explicit pointer).

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Use of Android WebView (super detailed usage)

1.1 overview of WebView Android WebView is a special view on the Android platform. It can be used to display web pages. This WebView class can be used to display only one online web page in the app. Of course, it can also be used to develop browsers. The internal implementation of WebView uses WebKit […]