Go language vs. C + + Reference Pass

Source: Internet
Author: User
This article mainly describes the go in the end there is no reference to the parameters (compared to C + +), the need for friends can refer to the following

Three ways to pass parameters in C + +

Value passing:

One of the most common ways to pass a parameter is a copy of the argument, which changes the parameter in the function without affecting the parameters outside the function. It is generally a function that modifies the parameters internally and does not want to affect the caller by passing the value.

Pointer passing

A parameter is a pointer to the address of an actual parameter, as the name implies, the action that the parameter points to in the function, and the argument itself is modified.

Reference delivery

In C + +, a reference is the alias of a variable, which is actually the same thing, and the same address exists in memory. In other words, regardless of where the reference operation is, the referenced variable is manipulated fairly directly.

Here's the demo:


#include <iostream>//value passed void func1 (int a) {std::cout << "value passed, variable address:" << &a << ", Variable value:" &LT;&L T  a << Std::endl; A + +;}  Pointer passed void Func2 (int* a) {std::cout << "pointer pass, variable address:" << a << ", Variable Value:" << *a << Std::endl; *a = *a + 1;} Reference passed void Func3 (int& a) {std::cout << "pointer pass, variable address:" << &a << ", Variable Value:" << a << std  :: Endl; A + +;}  int main () {int a = 5;  Std::cout << "Variable actual address:" << &a << ", Variable Value:" << a << Std::endl;  Func1 (a);  Std::cout << "Value pass operation, variable value:" << a << Std::endl;  Std::cout << "Variable actual address:" << &a << ", Variable Value:" << a << Std::endl;  Func2 (&a);  Std::cout << "Pointer pass operation, variable value:" << a << Std::endl;  Std::cout << "Variable actual address:" << &a << ", Variable Value:" << a << Std::endl;  Func3 (a);  Std::cout << "Reference pass operation, variable value:" << a << Std::endl; return 0;}

The output results are as follows:

Variable actual address: 0X28FEAC, variable value: 5
Value pass, variable address: 0X28FE90, variable value: 5
Value after the pass operation, the value of the variable: 5
Variable actual address: 0X28FEAC, variable value: 5
Pointer passing, variable address: 0X28FEAC, variable value: 5
After a pointer pass operation, the value of the variable: 6
Variable actual address: 0X28FEAC, variable value: 6
Pointer passing, variable address: 0X28FEAC, variable value: 6
After the reference pass operation, the value of the variable: 7

Parameter Passing in Go

The above describes the C + + three kinds of parameter passing way, value transfer and pointer passing is easy to understand, then Go is not also have these methods of communication? This has been controversial, but compared to the concept of C + + reference passing, we can say that Go does not have a reference delivery method. Why do you say that, because Go does not have the concept of a variable reference. But go has a reference type, which is explained later.

Let's look at an example of Go value and pass pointer:


Package Mainimport (  "FMT") func main () {  A: = 1  fmt. PRINTLN ("Variable actual address:", &a, "Variable value:", a)  func1 (a)  FMT. PRINTLN ("Value passed after operation, variable value:", a)  FMT. PRINTLN ("Variable actual address:", &a, "Variable value:", a)  Func2 (&a)  FMT. PRINTLN ("Pointer pass operation, variable value:", a)}//value passed func func1 (a int) {  a++  fmt. PRINTLN ("value passed, variable address:", &a, "Variable value:", a)}//pointer passing func FUNC2 (a *int) {  *a = *a + 1  fmt. PRINTLN ("pointer pass, variable address:", A, "Variable value:", *a)}

The output results are as follows:

Variable actual address: 0xc04203c1d0 variable Value: 1
Value pass, variable address: 0xc04203c210 variable Value: 2
Value after the pass operation, the value of the variable: 1
Variable actual address: 0xc04203c1d0 variable Value: 1
Pointer passing, variable address: 0xc04203c1d0 variable Value: 2
After a pointer pass operation, the value of the variable: 2
As you can see, the Go primitive type's value passing and pointer passing is no different from C + +, but it does not have the concept of a variable reference. How do you understand the reference type of Go?

Reference type of Go

In Go, reference types include slices, dictionaries, channels, and so on. In the case of slices, is it a reference to pass a slice?

As an example:


Package Mainimport (  "FMT") func main () {  M1: = Make ([]string, 1)  m1[0] = "Test"  FMT. Println ("Call FUNC1 before M1 value:", M1)  FUNC1 (M1)  FMT. Println ("Call FUNC1 after M1 value:", M1)}func func1 (A []string) {  a[0] = "Val1"  fmt. Println ("Func1:", a)}

The output results are as follows:

Call FUNC1 before M1 value: [Test]

Func1 in: [Val1]

M1 value after calling Func1: [Val1]

The changes to the slices in the function affect the values of the actual parameters. Does that mean that the reference is passed?

In fact, to answer this question, we have to figure out whether the call function slicing M1 has changed. First, we need to recognize the nature of the Chu slice.

A slice is a description of an array fragment. It contains a pointer to an array, the length of the fragment.

That is, we are not printing the slice itself, but the array that the slice points to. To give an example, verify that the slices have changed in the end.


  Package Mainimport (  "FMT") func main () {  M1: = Make ([]string, 1)  m1[0] = "Test"  FMT. Println ("Call FUNC1 before M1 value:", M1, Cap (M1))  FUNC1 (M1)  FMT. Println ("Call FUNC1 after M1 value:", M1, Cap (M1))}func func1 (A []string) {  a = append (A, "Val1")  FMT. Println ("Func1:", A, Cap (a))}

The output results are as follows:

Call FUNC1 before M1 value: [Test] 1

FUNC1: [Test val1] 2

Call FUNC1 after M1 value: [Test] 1

This result shows that the slices have not changed before and after the call. The so-called "change" in the previous example is actually a change in the elements of the array that points to the array in the slice, which may be a bit of a mouthful, but in practice. Again, the reference type's arguments are not pass-by-reference.

To get a thorough understanding of a slice is a description of an array fragment. It contains a pointer to the array, the length of the fragment, which is interesting to see in this article: Http://www.jb51.net/kf/201604/499045.html. Learn about the sliced memory model.

Summarize

The summary is very simple, the language also needs through the phenomenon to see the essence. And the conclusions of this article need to be remembered:

There is no pass-by-reference in Go.

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.