当前位置:网站首页>Will golang share data with fragment append
Will golang share data with fragment append
2022-04-23 15:04:00 【Mrpre】
Golang Bisection append Whether data will be shared
Golang The official response to slice See the article for the description of :https://blog.golang.org/slices-intro Most, especially the first half , It's very detailed , But finally about append The relevant part , It makes people feel a little tasted .
First , Write the conclusion first :append Back to slice object , It is possible to share input parameters slice The bottom of the ( It means that for the returned object slice The assignment of will affect the input parameters slice) It is also possible not to share input parameters slice The bottom of the .
Let's start with a few examples
example 1:
package main
import (
"fmt"
)
func main() {
s := make([]int, 3)
s[1] = 1000
fmt.Println(len(s), cap(s), s)
}
The result is printed out 3 3 [0 1000 0]
, There is no dispute about this .
example 2:
package main
import (
"fmt"
)
func main() {
s1 := make([]int, 3)
s2 := append(s1, 1000)//s1 Additional value 1000, The return value is s2
fmt.Println("s2", len(s2), cap(s2), s2)
fmt.Println("s1", len(s1), cap(s1), s1)
fmt.Println("After modify s2")
s2[1] = 999// Yes s2 assignment , See if it's right s1 Have an impact on
fmt.Println("s2", len(s2), cap(s2), s2)
fmt.Println("s1", len(s1), cap(s1), s1)
}
Print the results
s2 4 6 [0 0 0 1000]
s1 3 3 [0 0 0]
After modify s2
s2 4 6 [0 999 0 1000]
s1 3 3 [0 0 0]
Seems to be , We are right. s2 Assignment (s2[1] = 999) No impact s1, therefore ,append Returned object , Just like s1 It doesn't matter ?NO, Let's take an example 3
example 3:
package main
import (
"fmt"
)
func main() {
s1 := make([]int, 3)
s_slice := s[1:2]
s2 := append(s_slice, 1000)
fmt.Println("s2", len(s2), cap(s2), s2)
fmt.Println("s1", len(s1), cap(s1), s1)
fmt.Println("After modify s2")
s2[1] = 999
fmt.Println("s2", len(s2), cap(s2), s2)
fmt.Println("s1", len(s1), cap(s1), s1)
}
s2 2 2 [0 1000]
s1 3 3 [0 0 1000]
After modify s2
s2 2 2 [0 999]
s1 3 3 [0 0 999]
From the results , You see s1 Modified synchronously . example 3 and example 2 The only difference is append Input ,s_slice and s1, here Let's look back at the official documents mentioned earlier in the article :https://blog.golang.org/slices-intro The properties of the shard include 3 individual ( Array pointer 、cap、len), For example 3 in ,s_slice and s1 The difference is He two cap len Different ( For relevant knowledge, you can search other articles or directly see the links given above ).
therefore , You can guess. ,append Yes cap perhaps len 了 . cap Express 了 slice Maximum storage capacity ,len Represents the current number of elements .
len(s1), cap(s1), All are 3, Express s1 Capacity is 3, The number of elements is 3, It indicates that the s1 Full of , you are here s1 On the operation append, Then you must create a new array pointer , That is an example 2 in ,s2-> Array pointer and s1-> Array pointer Completely independent . That's why append Enter the reference yes s1 when , For returned s2 The operation of , It won't affect s1.
go back to example 3, Enter the reference s_slice Of cap yes 2,len yes 1( because s_slice Derived from s1,s1 Capacity is 3,len yes 3,s_slice take [1:2],cap There's one left ), So at this time s_slice It has capacity .
For those with capacity slice,append operation , It's in the present Insert a value after the pointer array . That means ,s2 Used s1 Pointer array of , modify s1 It will be modified synchronously s1.
append The pseudo code of the implementation can be understood as
append(src, other) {
ret = src
// Excess capacity , Just apply for new memory , Otherwise returns the value of the object byte still src Of byte
if len(other) + src->len > src->cap {
byte = new(...);
copy(byte, src->byte)
ret->byte = byte//replaced by new buffer
} //else ret->byte == src->byte
copy(src->byte + src->len, other)
}
append There are two faces .
版权声明
本文为[Mrpre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231409587992.html
边栏推荐
- js——實現點擊複制功能
- How to upload large files quickly?
- Flink datastream type system typeinformation
- 三、梯度下降求解最小θ
- Epoll's et, lt working mode -- example program
- async关键字
- Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
- Five data types of redis
- JUC学习记录(2022.4.22)
- UML project example -- UML diagram description of tiktok
猜你喜欢
Mds55-16-asemi rectifier module mds55-16
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
[NLP] HMM hidden Markov + Viterbi word segmentation
win10 任务栏通知区图标不见了
How to upload large files quickly?
LeetCode 练习——396. 旋转函数
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
Thread synchronization, life cycle
UML学习_day2
分布式事务Seata介绍
随机推荐
epoll 的EPOLLONESHOT 事件———实例程序
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
QT Detailed explanation of pro file
When splicing HQL, the new field does not appear in the construction method
I/O复用的高级应用:同时处理 TCP 和 UDP 服务
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
每日一题-LeetCode396-旋转函数-递推
How to design a good API interface?
[jz46 translate numbers into strings]
Go basic reflection
1990年1月1日是星期一,定义函数date_to_week(year,month,day),实现功能输入年月日后返回星期几,例如date_to_week(2020,11,1),返回:星期日。 提示:
Programming philosophy - automatic loading, dependency injection and control inversion
Model location setting in GIS data processing -cesium
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
The win10 taskbar notification area icon is missing
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
Async keyword
Leetcode exercise - 396 Rotation function
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system