Parameter passing in the Go language

Source: Internet
Author: User

In the case of a language, there are two ways in which a function is passed, one is value passing, and the other is a reference pass, so how does the go language pass the parameter
Value passing is a copy of the passed variable, reference passing refers to the address of the passing of the variable, in the value of the process, the function of the function parameter modification, will not lead to the modification of the argument, and reference to the parameter modification, will lead to the modification of the argument, this is in other languages in the past, some of the cognition, Then the go language will subvert our perception of the past and use a new approach.

    • Demo1
package mainimport (    "fmt")func main() {    a := 10    fmt.Println("实参的地址",&a)    update(a)    fmt.Println(a)}func update(b int) {    b = 1    fmt.Println("形参的地址",&b)}// 运行结果实参的地址 0x10414020形参的地址 0x1041402410

By the demo, we can conclude that when the value is passed, the parameter is a copy of the argument

    • Demo2
package mainimport (    "fmt")func main() {    a := 10    p := &a    fmt.Println("指针的地址",&p)    fmt.Println("实参的地址",&a)    update(p)    fmt.Println(a)}func update(b *int) {    *b = 1    fmt.Println("形参的指针地址",&b)    fmt.Println("形参的地址",b)}// 运行结果指针的地址 0x1040c128实参的地址 0x10414020形参的指针地址 0x1040c138形参的地址 0x104140201

It is found that when the pointer is passed, the pointer address of the argument and the formal parameter are not the same value, but it points to the same address, so that when the pointer is passed, the go language passes the copy of the pointer, which is a way of passing the value.

    • Demo3

And look at a complex, structural body.

package mainimport (    "fmt")type Person struct {    Name string}func main() {    p := Person{"zhang san"}    fmt.Printf("实参的地址:%p\n", &p)    update(p)    fmt.Println(p)}func update(p Person) {    p.Name = "li si"    fmt.Printf("形参的地址:%p\n", &p)}// 输出结果实参的地址:0x1040c128形参的地址:0x1040c138{zhang san}
    • Demo4

Pointer passing

package mainimport (    "fmt")type Person struct {    Name string}func main() {    p := Person{"zhang san"}    pr := &p    fmt.Printf("实参的指针地址:%p\n", &pr)    fmt.Printf("实参的地址:%p\n", &p)    update(pr)    fmt.Println(p)}func update(p *Person) {    p.Name = "li si"    fmt.Printf("形参的指针地址:%p\n", &p)    fmt.Printf("形参的地址:%p\n", p)}// 运行结果实参的指针地址:0x1040c130实参的地址:0x1040c128形参的指针地址:0x1040c140形参的地址:0x1040c128{li si}

By comparison between Demo3 and Demo4, the analysis can be obtained, when the value is passed, a copy of the value's argument is passed, and when the pointer is passed, a copy of the pointer is passed, which points to the address of the argument

Through the experiments above, it can be concluded that both the value passing and the pointer passing are the corresponding copies. In the go language, only one value is passed.

    • Demo5
package mainimport (    "fmt")type Person struct {    Name string}func main() {    persons:=make(map[string]int)    persons["张三"]=19    //mp:=&persons    fmt.Printf("原始map的内存地址是:%p\n",&persons)    update(persons)    fmt.Println("map值被修改了,新值为:",persons)}func update(p map[string]int) {    fmt.Printf("函数里接收到map的内存地址是:%p\n",&p)    p["张三"]=20}// 运行结果原始map的内存地址是:0x1040c128函数里接收到map的内存地址是:0x1040c138map值被修改了,新值为: map[张三:20]

The demo run result is inconsistent with our previous inference result, there is a special make in the code, so we have reason to suspect that the result of make is a pointer type, which conforms to our inference.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.