Go OR value passed passed by reference

What is passed by value (value transfer)

Meaning traditional values are: always a copy of the original function of this thing passed, a copy. For example, we pass a inttype parameter passed is actually a copy of this parameter; pass a pointer type of argument, that this in fact deliver a copy of the pointer, rather than the pointer value.

For this type of foundation type int we can well understand that they are a copy, but the pointer it? We feel that we can modify the original value by it, how is it a copy? Let's look at an example.

func main() {
	i:=10
	ip:=&i
	fmt.Printf("原始指针的内存地址是:%p\n",&ip)
	modify(ip)
	fmt.Println("int值被修改了,新值为:",i)
}

 func modify(ip *int){
	 fmt.Printf("函数里接收到的指针的内存地址是:%p\n",&ip)
 	*ip=1
 }

We run, you can see the input results are as follows:

原始指针的内存地址是:0xc42000c028
函数里接收到的指针的内存地址是:0xc42000c038
int值被修改了,新值为: 1

First of all we need to know, anything stored in memory has its own address, the pointer is no exception, although it points to other data, but also store the pointer memory.

So we can see the output, which is a copy of the pointer, two pointers as stored in the memory address are different, although the same value of the pointer, but are two different pointers.

What is the by-reference (pass by reference)

Go Language (Golang) is not passed by reference, here I can not use the Go For instance sub, but can be explained below.

 

summary

All parameters are passed by value transfer (by value), Go is a copy of the language, a copy. Because the content copied sometimes non-reference type (int, string, struct, etc. of these), so that the original content can not be modified in the function data; some reference type (pointer, map, slice, chan like these), can be modified so that original content data.

Can I modify the content of the original data, and by value, pass by reference is not necessarily related. In C ++, passed by reference certainly you can modify the original content data, in the Go language, although only traditional values, but we can also modify the original content data, because the parameter is a reference type.

Here, too, remember, and pass by reference is a reference type two concepts.

Remember again, Go, only pass value (value transfer).

Referring specifically to: https://www.flysnow.org/2018/02/24/golang-function-parameters-passed-by-value.html

Value types and reference types

Value Type: int, float, bool and string values ​​are all types of these types, the use of these types of variable values ​​in memory directly to exist, the value of the variable value stored in the stack type. When using the = sign value of one variable to another variable, such as i = J, is actually a copy of the value of i in the memory. You can get the memory address of the variable i by & i. Value copy

Reference Type: The specific slice, map, channel three predefined types. Reference type has a more complex storage structures: (1) allocates memory (2) initializing a set of attributes and the like of a reference type variable r1 is stored in the memory address where the value r1 (digital), or memory address of the first word location is located, it is referred to as the memory address pointer that actually stored in a separate one word. 

The main difference between the two: the copy operation and function of mass participation

Knowledge extension

1. Why use a pointer
1.1 per use a programming language pointers
C ++ pointer will be exposed to the user (programmer), and java and C # language such as the pointer hidden.
Little necessity and 1.2 pointers:
a pointer can effectively shows the data structure
can be dynamically allocated memory, to achieve the free memory management
can be easy to string
pointer address data stored directly related. For example: the value of the address transfer is better transmission efficiency, because the value of the argument passed start address value, then substituting the parameter assigned to the function calculation. Put the pointer address parameter arguments directly to the address, data taken directly use efficiency. (Where parameter changes will affect the value of the argument)
2. The difference between the reference pointer and
2.1 nature:
reference is alias address pointer is
2.2 specifically:
2.2.1 The face of:
the pointer can be changed at run time points value, and after once with an object reference bindings do not change. Means: pointer can be reassigned to point to another object, but the reference is always to be specified when the object is initialized, the future can not be changed, but the content can be changed to point

 

Released six original articles · won praise 0 · Views 211

Guess you like

Origin blog.csdn.net/jingshuipengpeng/article/details/105169561