当前位置:网站首页>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
边栏推荐
- Nuxt project: Global get process Env information
- Leetcode153 - find the minimum value in the rotation sort array - array - binary search
- OPPO数据湖统一存储技术实践
- like和regexp差别
- Programming philosophy - automatic loading, dependency injection and control inversion
- Advanced version of array simulation queue - ring queue (real queuing)
- 脏读、不可重复读和幻读介绍
- How to use OCR in 5 minutes
- [untitled]
- 免费在upic中设置OneDrive或Google Drive作为图床
猜你喜欢
Introduction to Arduino for esp8266 serial port function
8.2 text preprocessing
LeetCode149-直线上最多的点数-数学-哈希表
Nuxt project: Global get process Env information
we引用My97DatePicker 实现时间插件使用
3、 Gradient descent solution θ
Don't you know the usage scenario of the responsibility chain model?
The art of automation
Leetcode162 - find peak - dichotomy - array
How to use OCR in 5 minutes
随机推荐
Detailed explanation of C language knowledge points -- data types and variables [1] - carry counting system
封面和标题中的关键词怎么写?做自媒体为什么视频没有播放量
Difference between like and regexp
Lotus DB design and Implementation - 1 Basic Concepts
For 22 years, you didn't know the file contained vulnerabilities?
ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
Leetcode165 compare version number double pointer string
LeetCode165-比较版本号-双指针-字符串
冰冰学习笔记:一步一步带你实现顺序表
QT Detailed explanation of pro file
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
LeetCode151-颠倒字符串中的单词-字符串-模拟
Frame synchronization implementation
js——实现点击复制功能
The difference between having and where in SQL
asp. Net method of sending mail using mailmessage
What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
Mds55-16-asemi rectifier module mds55-16
Nuxt project: Global get process Env information
The win10 taskbar notification area icon is missing