Object-oriented programming emphasizes the binding of data and operations. The method is stateful and may modify the data itself. Therefore, it is particularly critical to determine whether the method will modify the original data during programming. Most developers who switch from other languages to the Go language will first understand whether the Go language passes parameters by "pass by value" or "pass by reference." If the first development language is a C language or C++ developer, it will also distinguish when to "pass pointer".
concept
What is memory?
Think of the memory as a series of cells, arranged one by one in a row, and each cell has a unique number. The numbering order is increasing, representing the memory location, which is the memory address.
Each cell can store a value, and the previously written value in the cell can be read and replaced by the number.
What is a variable?
What's bad is that if you use numbered programming directly, you need to manage the memory yourself, and it is difficult to run with other programs at the same time, which greatly increases the difficulty of writing large programs.
In order to solve this problem, it is necessary to introduce the concept of "variables". Variables are simply numbered pseudonyms, tags, or nicknames.
var a = 6
var b = a * 3
What is a pointer?
The value of the pointer is the number of another variable. The pointer points to the memory address of the variable, just as the memory address of the variable represents the value.
func main() {
a := 200
b := &a
*b++
fmt.Println(a)
}
What is a reference?
In the C++ language, an alias is declared for an existing variable, which is called a reference variable. As shown in the code, the three variables a, b, and c all share the same memory address
#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;
}
Go language has no reference types
Go programs can create variables that point to a uniform memory address, but they cannot create two variables that share the same memory address. As shown in the code, b and c have the same value (address of a), but the memory addresses of b and c are unique. Updating the content of b has no effect on c.
package main
import "fmt"
func main() {
var a int
var b, c = &a, &a
fmt.Println(b, c) // 0x1040a124 0x1040a124
fmt.Println(&b, &c) // 0x1040c108 0x1040c110
}
On April 3, 2013, the concept of "reference type" has been completely deleted from the Go specification. , now the "reference" in the Go specification does not represent the concept of "reference type".
Is a pointer a type?
Is a pointer a type? This question may be quite different in C or C++, but it is very clear in the Go language. There is no doubt: is .
A pointer type denotes the set of all pointers to variables of a given type, called the base type of the pointer. The value of an uninitialized pointer is nil.
Since there are types, there are instances of types: value, type and value are two completely different concepts. Obviously, in the Go language, when it comes to pointers, it includes "pointer type" and "pointer value".
At the same time, in order to avoid the risk of pointer introduction, Go language has made many constraints on pointers, as follows:
- Pointer value does not support mathematical operations
var p *int
p++
You cannot change the position pointed to by the pointer unless you assign another address to it. At the same time, Array-Pointer Duality is not supported.
- Pointer values with different base types cannot be directly assigned to each other
type MyInt int64
type Ta *int64
type Tb *MyInt
// 4 nil pointers of different types.
var pa0 Ta
var pa1 *int64
var pb0 Tb
var pb1 *MyInt
// None of the following 3 lines compile ok.
/*
_ = pa0 == pb0
_ = pa1 == pb1
_ = pa0 == Tb(nil)
*/
- It is safe to return a pointer to a local variable
func f() *int {
i := 1
return &i
}
Pointer summary
Developers who have used the C/C++ language, the unsafe pointer used to use is unsafe.Pointer
instead of ordinary pointers.
int* valFirst
intptr_t valSecond
void* valThird
vs
int* valFirst
type intptr_t *int
intptr_t valSecond
unsafe.Pointer valThird
Contrast taking, the effect is better.
The author of this article : cyningsun
address of this article is : https://www.cyningsun.com/08-16-2021/go-has-no-reference-and-safe-pointer.html
Copyright Statement : All articles in this blog, except for special statements, use CC BY-NC-ND 3.0 CN license agreement. Please indicate the source!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。