好好爱自己!

golang 中Pointers Vs References

原文: https://spf13.com/post/go-pointers-vs-references/

Pointers Vs References

Some languages including C, C++ support pointers. Other languages including C++, Java, Python, Ruby, Perl and PHP all support references. On the surface both references and pointers are very similar, both are used to have one variable provide access to another. With both providing a lot of the same capabilities, it’s often unclear what is different between these different mechanisms. In this article I will illustrate the difference between pointers and references.

Why Does This Matter

Pointers are at the very core of effective Go. Most programmers are learning Go with a foundation in one of the languages mentioned above. Consequently understanding the difference between pointers and references is critical to understanding Go. Even if you are coming from a language that uses pointers, Go’s implementation of pointers differs from C and C++ in that it retains some of the nice properties of references while retaining the power of pointers.

The remainder of this article is written with the intent of speaking broadly about the concept of references rather than about a specific implementation. We will be using Go as the reference implementation for pointers.

What Is The Difference?

A pointer is a variable which stores the address of another variable.

A reference is a variable which refers to another variable.

To illustrate our point, use the following example in C++ which supports both pointers and references.

int i = 3;
int *ptr = &i;
int &ref = i;

The first line simply defines a variable. The second defines a pointer to that variable’s memory address. The third defines a reference to the first variable.

Not only are the operators different, but you use the differently as well. With pointers must use the * operator to dereference it. With a reference no operator is required. It is understood that you are intending to work with the referred variable.

Continuing with our example, the following two lines will both change the value of i to 13.

*ptr = 13;
ref = 13;

You may be asking, what happens if I try to access the ptr directly without dereferencing first. This takes us to our second critical difference between pointers and references. Pointers can be reassigned while references cannot. In other words, a pointer can be assigned to a different address.

Consider the following example in Go:

package main

import "fmt"

var ap *int

func main() {
    a := 1          // define int
    b := 2          // define int

    ap = &a
     // set ap to address of a (&a)
     //   ap address: 0x2101f1018
     //   ap value  : 1

    *ap = 3
     // change the value at address &a to 3
     //   ap address: 0x2101f1018
     //   ap value  : 3

    a = 4
     // change the value of a to 4
     //   ap address: 0x2101f1018
     //   ap value  : 4

    ap = &b
     // set ap to the address of b (&b)
     //   ap address: 0x2101f1020
     //   ap value  : 2
}

So far you could do all of the above in a reasonably similar manner using references, and often with a simpler syntax.

Stay with me, the following example will illustrate why pointers are more powerful than references.

Extending the function above:

    ...

    ap2 := ap
     // set ap2 to the address in ap
     //   ap  address: 0x2101f1020
     //   ap  value  : 2
     //   ap2 address: 0x2101f1020
     //   ap2 value  : 2

    *ap = 5
     // change the value at the address &b to 5
     //   ap  address: 0x2101f1020
     //   ap  value  : 5
     //   ap2 address: 0x2101f1020
     //   ap2 value  : 5
    // If this was a reference ap & ap2 would now
    // have different values

    ap = &a
     // change ap to address of a (&a)
     //   ap  address: 0x2101f1018
     //   ap  value  : 4
     //   ap2 address: 0x2101f1020
     //   ap2 value  : 5
    // Since we've changed the address of ap, it now
    // has a different value then ap2
}

You can experiment and play yourself at go play: http://play.golang.org/p/XJtdLxFoeO

The key to understanding the difference is in the second example.

If we were working with references we would not be able to change the value of b through *ap and have that reflected in *ap2. This is because once you make a copy of a reference they are now independent. While they may be referring to the same variable, when you manipulate the reference it will change what it refers to, rather than the referring value.

The final example demonstrates the behavior when you change the assignment of one of the pointers to point to a new address. Due to the limitations of references this is the only operation available.

Stay tuned… Next post will feature another property exclusively available to pointers, the pointer pointer.

For more information on pointers I’ve found the following resources helpful

--------------------------------------------

golang 中引用和指针的区别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
 
import "fmt"
 
var ap *int
 
func main() {
    a := 1 // define int
    b := 2 // define int
 
    ap = &a
    fmt.Println("set ap to address of a (&a)")
    //   ap address: 0x2101f1018
    //   ap value  : 1
    fmt.Println("ap address:", ap)
    fmt.Println("ap value:  ", *ap)
 
    *ap = 3
    fmt.Println("change the value at address &a to 3")
    //   ap address: 0x2101f1018
    //   ap value  : 3
    fmt.Println("ap address:", ap)
    fmt.Println("ap value:  ", *ap)
 
    a = 4
    fmt.Println("change the value of a to 4")
    //   ap address: 0x2101f1018
    //   ap value  : 4
    fmt.Println("ap address:", ap)
    fmt.Println("ap value:  ", *ap)
 
    ap = &b
    fmt.Println("set ap to the address of b (&b)")
    //   ap address: 0x2101f1020
    //   ap value  : 2
    fmt.Println("ap address:", ap)
    fmt.Println("ap value:  ", *ap)
 
    ap2 := ap
    fmt.Println("set ap2 to the address in ap")
    //   ap  address: 0x2101f1020
    //   ap  value  : 2
    //   ap2 address: 0x2101f1020
    //   ap2 value  : 2
    fmt.Println("ap address: ", ap)
    fmt.Println("ap value:   ", *ap)
    fmt.Println("ap2 address:", ap2)
    fmt.Println("ap2 value:  ", *ap2)
 
    *ap = 5
    fmt.Println("change the value at the address &b to 5")
    //   ap  address: 0x2101f1020
    //   ap  value  : 5
    //   ap2 address: 0x2101f1020
    //   ap2 value  : 5
    // If this was a reference ap & ap2 would now
    // have different values
    fmt.Println("ap address: ", ap)
    fmt.Println("ap value:   ", *ap)
    fmt.Println("ap2 address:", ap2)
    fmt.Println("ap2 value:  ", *ap2)
 
    ap = &a
    fmt.Println("change ap to address of a (&a)")
    //   ap  address: 0x2101f1018
    //   ap  value  : 4
    //   ap2 address: 0x2101f1020
    //   ap2 value  : 5
    // Since we've changed the address of ap, it now
    // has a different value then ap2
    fmt.Println("ap address: ", ap)
    fmt.Println("ap value:   ", *ap)
    fmt.Println("ap2 address:", ap2)
    fmt.Println("ap2 value:  ", *ap2)
}

  

posted @   立志做一个好的程序员  阅读(525)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2018-05-28 【转】javascript 的类,原型,继承的理解
2018-05-28 javascript event loop
2015-05-28 angular 实现modal windows效果(即模态窗口,半透明的遮罩层),以及bootstrap(css,components,js)的初步学习

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示