Brief analysis of Golang pointer

Source: Internet
Author: User
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

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.