Skip to content

Commit 2b81785

Browse files
committed
update
1 parent 472ef8d commit 2b81785

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

EN/pointer.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
# pointer
22

33

4+
### Golang there is no reference to pass parameters, all are value pass
5+
6+
```
7+
package main
8+
9+
import "fmt"
10+
11+
func main() {
12+
var a int
13+
var b, c = &a, &a
14+
fmt.Println(b, c) // 0xc0000160d0 0xc0000160d0
15+
fmt.Println(&b, &c) // 0xc00000e028 0xc00000e030
16+
}
17+
```
18+
Summary:
19+
* it is impossible for two variables to share a block of memory in a Golang project, but it is possible to create two variables pointing to the same memory address, which is not the same as two variables sharing a block of memory.
20+
* Golang is all value passing, value copying or parameter generating a new pointer address to the value of the incoming parameter. Copy the value or manipulate the value of the same parameter through another pointer.
21+
* Map and Channel not reference.
22+
423
### Pointer-based methods
524

625
When a function is called, each parameter value is copied. If a function needs to update a variable, or one of the function's parameters is too large, we hope to avoid such a default copy. In this case, we will Need to use pointers. Corresponding to the method we use to update the receiver object here, when the receiver variable itself is relatively large, we can use its pointer instead of the object to declare the method, as follows:

zh_CN/pointer.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
# pointer 指针
22

33

4+
### Golang 中不存在引用传参,全部是值传递
5+
6+
```
7+
package main
8+
9+
import "fmt"
10+
11+
func main() {
12+
var a int
13+
var b, c = &a, &a
14+
fmt.Println(b, c) // 0xc0000160d0 0xc0000160d0
15+
fmt.Println(&b, &c) // 0xc00000e028 0xc00000e030
16+
}
17+
```
18+
小结:
19+
* 在 Golang 项目中不可能存在两个变量共享一块内存,但是可以创建两个变量指向相同的内存地址,但这和两个变量共享一块内存是不一样的。
20+
* Golang传参全部是值传递,值拷贝或者用参数生成一个新的指针地址指向传入参数的值。即值拷贝或者通过另外一个指针操作同一个参数的值。
21+
* Map 和 Channel 不是引用。
22+
423
### 基于指针对象的方法
524

625
当调用一个函数时,会对其每一个参数值进行拷贝,如果一个函数需要更新一个变量,或者函数的其中一个参数实在太大我们希望能够避免进行这种默认的拷贝,这种情况下我们就需要用到指针了。对应到我们这里用来更新接收器的对象的方法,当这个接受者变量本身比较大时,我们就可以用其指针而不是对象来声明方法,如下:

0 commit comments

Comments
 (0)