Value Types and Reference Types
Value types: int, float, bool and string are all value types. These types of variables are directly pointing to values that exist in memory. The values of the variables of the value type are stored on the stack. When using the equal sign = to assign the value of one variable to another variable, such as j = i , the value of i is actually copied in memory. The memory address of the variable i can be obtained through &i. Value copy
Reference type: specifically refers to three predefined types: slice, map, and channel. Reference types have a more complex storage structure: (1) Allocate memory (2) Initialize a series of attributes and other variables of reference type r1 store the memory address (number) where the value of r1 is located, or the location of the first word in the memory address. This memory address is called a pointer, and this pointer is actually also present in another word.
The main differences between the two: copy operations and function parameters.
The main text begins with the focus of introducing the reference types in go.
First of all, the assignments in go are all value passing
a := 1
b := ax := Struct{}
y := x
They all have independent space in memory, that is, the copy process, so the changes to a certain property of y will not affect x
So what should we do if we want two variables to point to the same memory? You can use the reference type:
y := &x
At this time, the type of y is *Struct . At this time, we can modify y. After the modification, x will also find changes, because y is now a reference type, which points to the memory where the x structure is located.
We can use:
= xxx
To directly call the reference type structure assignment, but it should be noted that this is the syntax sugar of go. It just helps us simplify the process of obtaining actual memory through pointers. The complete writing should be like this:
(*y).variable = xxx
*y is a reverse reference to a pointer, which can be understood as *y == x.
Why is this syntax sugar designed? It is because in go we cannot directly operate pointers, such as in c++, directly compute memory addresses to obtain other memory addresses, which is not supported by default in go.
print(y) // Get memory address data like 0x8123
// In theory, you can get a new memory address, but it is not supported by default in go
newAddr := y + 4
Because the address cannot be operated directly, go provides syntax sugar, so that when we use the reference type to operate, the default is to operate the memory address pointed to by the reference.
Note that we can directly assign values to reference types, but the type of assignment must also be reference types.
y = &Struct{} // This is OK, but it cannot be y = Struct{}
a := 1
b := &a
b = 2 // This is not possible because the type of b is *int
Special reference types
The ones that can be created through the make() function are reference types, such as slice and map. Although slice looks like an array, it is actually a pointer type to the array memory space.
type Slice struct { point Point // Memory address len int cap int }
So we are executing:
a := []int
b = a
You will find that it seems that b and a point to the same array, and that is indeed the case. All assignments in go are value-passing, and the assignment of slice is also a copy of the slice object, that is, a and b are different slice objects, but they point to the same array
The same is true for maps, so I won’t talk about them much.
Summarize
This is all about this article about detailed explanation of the citation types in Go. For more citation types in Go, please search for my previous articles or continue browsing the related articles below. I hope you will support me in the future!