There is no reference passing in the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Original link: There is no pass-by-reference in Go

My previous post on pointers has caused a lot of controversy about citation passing. This post is a response to these controversies.

The first thing to be clear is that there is no reference variable in the Go language, so the go language does not have a reference to the syntax of the passed function call.

What is a reference variable?

In a language like C + + you can define an alias for an already existing variable, which is referred to as a reference variable.

#include <stdio.h>int main() {        int a = 10;        int &b = a;        int &c = b;        printf("%p %p %p\n", &a, &b, &c); // 0x7ffe114f0b14 0x7ffe114f0b14 0x7ffe114f0b14        return 0;}

You can see that a, B, and C all point to the same memory address, and writing data to a will change the contents of B and C. This is critical when you want to define reference variables in different scopes (that is, function calls).

There are no reference variables in the Go language

Unlike C + +, each defined variable in the Go language occupies a unique memory address

Dave's original example, I think the comparison with the above C + + example is not very prominent, so I changed a bit

package mainimport (    "fmt")func main() {    var a int    var b = &a    var c = &a    fmt.Println(&a, &b, &c) //0xc0420361d0 0xc04204e018 0xc04204e020}

In the Go language, it is not possible to create 2 variables and these 2 variables have the same memory address. The Go language is allowed to create 2 variables whose content is the same pointer to the same address, but it is completely different to share the same memory address with two variables.

package mainimport "fmt"func main() {        var a int        var b, c = &a, &a        fmt.Println(b, c)   // 0x1040a124 0x1040a124        fmt.Println(&b, &c) // 0x1040c108 0x1040c110}

In this example, B, C hold the same value is the address of a, however, B, c their own storage is stored in their own unique address, update B's content will not affect the content of C.

But Maps and Channels are reference variables.

Wrong! Maps and Channels are still not reference variables. If they are reference variables, then this program will print false.

package mainimport "fmt"func fn(m map[int]int) {        m = make(map[int]int)}func main() {        var m map[int]int        fn(m)        fmt.Println(m == nil)}

If M is a reference variable like C + +, the M defined in FN and the M defined in main should occupy the same memory address, but since the assignment in FN does not affect the value of M in main, we can see that the map is not a reference variable.

Conclusion:

There is no reference pass in the go language because there is no reference variable in the Go language.

Related Article

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.