This is a created article in which the information may have evolved or changed.
Pointers this thing, no matter in which language, is more difficult to understand the concept, in fact, as long as the explanation is clear, the concept of pointers is not difficult to understand.
The way to print addresses in Golang is: FMT. Printf ("%p", X), x represents the data structure to print
package mainimport "fmt"func main() { ss := []string{"a", "b", "c"} //小张=你 fmt.Printf("数据地址=%p,指针地址=%p\n", ss, &ss) ss1 := ss //张三=你 fmt.Printf("数据地址=%p,指针地址=%p\n", ss1, &ss1) ss = []string{"d"} //小张=另外一个同事 fmt.Printf("数据地址=%p,指针地址=%p\n", ss, &ss)}上述代码的打印结果:数据地址=0xc04203bd70,指针地址=0xc04203e3c0数据地址=0xc04203bd70,指针地址=0xc04203e440数据地址=0xc0420381e0,指针地址=0xc04203e3c0
We can see that the data address is the same, but the address of the variable itself is not the same. How does that make sense? From the perspective of life may be easier to explain: you in your department called Zhang San, also someone called you Zhang, or call you small three, assuming you are the only name in the department, the three, Zhang, small three all represent you this person. You are the data address that we said above, Zhang San, Xiao Zhang, small Three, are pointer variables, they all point to you this person.
The pointer is likely to change, for example, the Department of a new family surnamed Zhang, who is younger than you Zhang four, then in your department, the name of the small Zhang, pointing to become a four.
There are a few prerequisites for this hypothesis:
- Confined to a department, that is, a host.
- A name that only points to one person. That is, the pointer needs to point to a specific data