当前位置:网站首页>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
边栏推荐
- Have you learned the basic operation of circular queue?
- JUC learning record (2022.4.22)
- nuxt项目:全局获取process.env信息
- Async keyword
- How to write the keywords in the cover and title? As we media, why is there no video playback
- LeetCode165-比较版本号-双指针-字符串
- 我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
- 填充每个节点的下一个右侧节点指针 II [经典层次遍历 | 视为链表 ]
- Unity_ Code mode add binding button click event
- Sword finger offer II 019 Delete at most one character to get palindrome (simple)
猜你喜欢
thinkphp5+数据大屏展示效果
[NLP] HMM hidden Markov + Viterbi word segmentation
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
How to design a good API interface?
Model location setting in GIS data processing -cesium
What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
What is the role of the full connection layer?
如何设计一个良好的API接口?
MySQL error packet out of order
随机推荐
The art of automation
封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
Async void caused the program to crash
Redis master-slave synchronization
Is asemi ultrafast recovery diode interchangeable with Schottky diode
How to use OCR in 5 minutes
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
大文件如何快速上传?
One of the advanced applications of I / O reuse: non blocking connect -- implemented using select (or poll)
What is the role of the full connection layer?
分享3个使用工具,在家剪辑5个作品挣了400多
js——实现点击复制功能
Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
Daily question - leetcode396 - rotation function - recursion
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
编程哲学——自动加载、依赖注入与控制反转
Tencent has written a few words, Ali has written them all for a month
1-初识Go语言
Share 20 tips for ES6 that should not be missed
Chapter 7 of JVM series -- bytecode execution engine