当前位置:网站首页>The usage of slice and the difference between slice and array
The usage of slice and the difference between slice and array
2022-04-23 19:21:00 【Handsome that handsome】
slice yes go A linguistic feature of , It's kind of like cpp Of vector, Variable length , Can expand space . Today I read it in detail , Make a summary .
slice It's essentially an interval , The prototype is []T, The general implementation is as follows :
type slice struct {
first *T
len int
cap int
}
What you can see is a pointer to an array , So it's changing slice Will change the value of the array .
The usage difference between and array is not too big .
- Array based creation :
var myArrary [3]int = [3]int{
1,2,3}
var mySlice []int = myArray[:2] //[first:last] The way , Make up when you are short 0
- Create directly :
mySlice = make([]int, 5)// Created a 5 The initial value is 0 The section of
mySlice = make([]int, 5, 10)// Created a 5, Capacity of 10 The section of
- function :
len() Returns the number of elements
cap() Returns the size of the container
append() New elements
mySlice = append(mySlice, 1, 2, 3) // The parameter type of the latter is actually an indefinite parameter
mySlice2 = []int{
7, 8, 9}
mySlice = append(mySlice, mySlice2...) Join in ... It means that after breaking up, it is passed into
- copy() Deep copy slice
slice1 := []int{
1, 2, 3, 4, 5}
slice2 := []int{
1, 2, 3}
copy(slice2, slice1)//copy slice Before 3 Elements to slice2
slice2 = slice1 // Will copy a reference to slice2
- Different from array
func arrayModify(array [5]int) {
newArray := array
newArray[0] = 88
}
func sliceModify(slice []int) {
newSlice := slice
newSlice[0] = 88
}
func main() {
array := [5]int{
1, 2, 3, 4, 5}
slice := []int{
1, 2, 3, 4, 5}
arrayModify(array)
sliceModify(slice)
fmt.Println(array)
fmt.Println(slice)
}
// [1 2 3 4 5]
// [88 2 3 4 5]
What you can see is that the array passes values , Does not change the value of the element , This and c/cpp Different , And though slice Value semantics , But it's itself a pointer type , So it changes the value , But it doesn't mean slice The reference , This needs attention . The problems caused by value semantics will be discussed in detail later .
版权声明
本文为[Handsome that handsome]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210557451036.html
边栏推荐
- Zero base to build profit taking away CPS platform official account
- What is a message queue
- MySQL syntax collation (3)
- ArcGIS JS API dojoconfig configuration
- Codeworks round 783 (Div. 2) d problem solution
- 点云数据集常用处理
- 数据分析学习目录
- Speculation on the way to realize the smooth drag preview of video editing software
- JS calculation time difference
- OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
猜你喜欢

Intuitive understanding of the essence of two-dimensional rotation

ArcMap连接 arcgis server

Application of DCT transform

arcMap 发布切片服务

该买什么设备,Keysight 给你挑好了

redis优化系列(三)解决主从配置后的常见问题

Partage de la conception de l'alimentation électrique de commutation et illustration des compétences en conception de l'alimentation électrique

An idea of rendering pipeline based on FBO

2021-2022-2 ACM training team weekly Programming Competition (8) problem solution

Using oes texture + glsurfaceview + JNI to realize player picture processing based on OpenGL es
随机推荐
MySQL syntax collation (5) -- functions, stored procedures and triggers
SSDB基础
Transaction processing of SQL Server database
JS to get the local IP address
Gossip: on greed
数据分析学习目录
An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary
What is a message queue
网络协议之:sctp流控制传输协议
Class loading process of JVM
8266 obtain 18b20 temperature
Some speculation about the decline of adults' language learning ability
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
Steps to build a deep learning environment GPU
Audio signal processing and coding - 2.5.3 the discrete cosine transform
Is it safe to open an account in Bohai futures.
Lottery applet, mother no longer have to worry about who does the dishes (assign tasks), so easy
RuntimeError: Providing a bool or integral fill value without setting the optional `dtype` or `out`
Coordinate conversion WGS-84 to gcj-02 and gcj-02 to WGS-84
Strange passion