Home  >  Article  >  Backend Development  >  Examples of go value passing and address passing

Examples of go value passing and address passing

藏色散人
藏色散人forward
2020-10-30 15:00:202319browse

The following column will introduce you to examples of go value passing and address passing in the golang tutorial column. I hope it will be helpful to friends who need it!

Examples of go value passing and address passing

Let’s look at a simple example first

1. Add an & in front of the variable to indicate the address of the variable (that is, this Pointer to variable), adding * in front of the pointer variable indicates the value corresponding to this pointer (understand for yourself)

//main.gopackage main
func ByAss() {
  s := 100
  p := &s  // s 的地址给了p

  fmt.Println(*p) //100 所以p对应的值变成了100
  *p += 100
  fmt.Println(*p)  //200
  fmt.Println(s) //200 p和s相同的地址,所以s也变成200}//demo.gopackage main
func main() {
  ByAss()}

Go defaults to passing parameters by value, that is, passing a copy of the parameter. After the function receives a copy of the parameter, the value of the copy may be changed during the use of the variable, but it will not affect the original variable, such as Function(arg1).

2. Ordinary value passing (pay attention to this example, it looks very simple, but it is easy to ignore)

func ByValue1(n1 int) {
  //此时n1 是n 的一个拷贝,所以地址不一样
  fmt.Println(&n1)//0xc00000a098}package main
func main() {
  n := 100
  fmt.Println(&n) //0xc00000a090
  ByValue1(n)//传递副本}

If you want the function to directly modify the value of the parameter, and Instead of operating on a copy of the parameter, you need to pass the address of the parameter (add the ampersand in front of the variable name, such as &variable) to the function. This is passing by reference, such as Function(&arg1). At this time, what is passed to the function is a pointer. .

3. If you want to use the address to pass

func main() {
    n := 100
    fmt.Println(&n) //0xc00000a090
    ByReference(&n) //传递指针
    fmt.Println(n) //200}//注意函数 参数类型需要是用指针符号(*)func ByReference(n1 *int) {

   *n1 += 100
   fmt.Println(n1)//0xc00000a090 地址与n相同, 所以n值被改变了
  //注意
  //ip := &n1//这个写法有问题,表示地址的地址
  //fmt.Println(ip)}

4. Look at the array

  • (a) Array It is also passed by value
    arr := [5]int{1,3,5,6,7}arrByValue(&arr) //也需要使用& 符号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) //没有使用& 符号
    fmt.Print(arr[0])// 11 结果已经改变,说明数组是地址传递

    //就算不通过函数,也是引用传递
    //arr := []int{1,3,5,6,7}
    //arr2 := arr
    //arr2[1] = 0
    //fmt.Print(arr)//[1 0 5 6 7]}//aaafunc bbb(arr1 []int) {
   arr1[0] += 10}
  • (b) in When a function is called, reference types such as slices, dictionaries, maps, interfaces, and channels are passed by reference by default (even if the pointer is not explicitly pointed out).

The above is the detailed content of Examples of go value passing and address passing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete