当前位置:网站首页>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
边栏推荐
- select 同时接收普通数据 和 带外数据
- The difference between having and where in SQL
- The win10 taskbar notification area icon is missing
- Epoll's et, lt working mode -- example program
- 1 - first knowledge of go language
- Advanced version of array simulation queue - ring queue (real queuing)
- OPPO数据湖统一存储技术实践
- Thinkphp5 + data large screen display effect
- MySQL error packet out of order
- 多语言通信基础 06 go实现grpc的四种数据流模式实现
猜你喜欢
eolink 如何助力遠程辦公
三、梯度下降求解最小θ
8.4 realization of recurrent neural network from zero
Introduction to Arduino for esp8266 serial port function
22年了你还不知道文件包含漏洞?
8.2 text preprocessing
What is the main purpose of PCIe X1 slot?
3、 Gradient descent solution θ
asp. Net method of sending mail using mailmessage
Leetcode151 - invert words in string - String - simulation
随机推荐
SQL中HAVING和WHERE的区别
Epolloneshot event of epoll -- instance program
What is the main purpose of PCIe X1 slot?
Don't you know the usage scenario of the responsibility chain model?
async关键字
UML learning_ Day2
你还不知道责任链模式的使用场景吗?
Chapter 7 of JVM series -- bytecode execution engine
Comment eolink facilite le télétravail
[untitled]
JUC学习记录(2022.4.22)
Ffmpeg installation error: NASM / yasm not found or too old Use --disable-x86asm for a clipped build
Share 20 tips for ES6 that should not be missed
Explain TCP's three handshakes in detail
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
分享 20 个不容错过的 ES6 的技巧
Brute force of DVWA low -- > High
[proteus simulation] automatic range (range < 10V) switching digital voltmeter
【thymeleaf】处理空值和使用安全操作符
Swift protocol Association object resource name management multithreading GCD delay once