当前位置:网站首页>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
边栏推荐
- Redis主从同步
- Do (local scope), initializer, memory conflict, swift pointer, inout, unsafepointer, unsafebitcast, success
- Swift protocol Association object resource name management multithreading GCD delay once
- How to write the keywords in the cover and title? As we media, why is there no video playback
- 分享 20 个不容错过的 ES6 的技巧
- I/O复用的高级应用:同时处理 TCP 和 UDP 服务
- Daily question - leetcode396 - rotation function - recursion
- Advanced version of array simulation queue - ring queue (real queuing)
- Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
- Share 3 tools, edit 5 works at home and earn more than 400
猜你喜欢
Tencent has written a few words, Ali has written them all for a month
Swift protocol Association object resource name management multithreading GCD delay once
8.5 concise implementation of cyclic neural network
Bingbing learning notes: take you step by step to realize the sequence table
QT Detailed explanation of pro file
Leetcode162 - find peak - dichotomy - array
[NLP] HMM hidden Markov + Viterbi word segmentation
What is the main purpose of PCIe X1 slot?
Role of asemi rectifier module mdq100-16 in intelligent switching power supply
What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
随机推荐
我的 Raspberry Pi Zero 2W 折腾笔记,记录一些遇到的问题和解决办法
Alexnet model
Contraction mapping theorem
Introduction to Arduino for esp8266 serial port function
中富金石财富班29800效果如何?与专业投资者同行让投资更简单
冰冰学习笔记:一步一步带你实现顺序表
The art of automation
Is asemi ultrafast recovery diode interchangeable with Schottky diode
Comment eolink facilite le télétravail
Explanation and example application of the principle of logistic regression in machine learning
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
OPPO数据湖统一存储技术实践
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
LeetCode149-直线上最多的点数-数学-哈希表
How does eolink help telecommuting
Fill in the next right node pointer II of each node [classical hierarchy traversal | regarded as linked list]
UML project example -- UML diagram description of tiktok
[thymeleaf] handle null values and use safe operators
UML学习_day2
How to design a good API interface?