Go – & get address and * dereference

Time:2020-4-6

&Variable gets the address of the variable in memory space

*Variable address gets the value of a variable

1、 General data

package main

import "fmt"

func main(){
    b :=1111
    C: = & B // when getting the type of address C of B * int
    test(c)
    FMT. Println (b) // a value of 333 sent a change
}
func test(a *int){
    *a=333
}
//It can be compared with the following
func main(){
    b :=1111
    test(b)
    fmt.Println(b)
}
Func test (a int) {// if it is not an incoming address, it will open up a new memory space with no effect on the original value
    a=333
}

2、 Array and slice (slice is special)

// writing
package main

import "fmt"

func main(){
    b :=&[]int{1,2,3}
    test(b)
    fmt.Println(b)
}
func test(a *[]int){
    (*a)[1]=3
}

//Go optimizes slicing to omit content
package main

import "fmt"

func main(){
    b :=[]int{1,2,3}
    test(b)
    fmt.Println(b)
}
func test(a []int){
    a[1]=3
}
//If the passed in object is a value type, not a reference type, it will not take effect unless it is sliced to the reference type, and the array value type will not take effect. It can only be written in the same way

Recommended Today

Python basics Chinese series tutorial · translation completed

Original: Python basics Python tutorial Protocol: CC by-nc-sa 4.0 Welcome anyone to participate and improve: a person can go very fast, but a group of people can go further. Online reading Apache CN learning resources catalog introduce Seven reasons to learn Python Why Python is great Learn Python introduction Executing Python scripts variable character string […]