Golang Slicing Slice

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Sliced slice

    • It is not an array by itself, it points to the underlying array
package mainimport (    "fmt")func main() {    var s1 []int //这样的话就完成了slice的声明,如果是数组的话,必须在中括号当中必须有明确的数字或3个点    fmt.Println(s1)}
    • As an alternative to arrays, you can associate local or all of the underlying array
package mainimport (    "fmt")func main() {    var s1 []int //这样的话就完成了slice的声明,如果是数组的话,必须在中括号当中必须有明确的数字或3个点,这是个空slice    a := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}    fmt.Println(s1)    fmt.Println(a)    s2 := a[9]    s3 := a[5:10] //a[5,6,7,8,9],也可以写成a[5:]    fmt.Println(s2)    fmt.Println(s3)}
    • Its value is a reference type,
    • You can create directly or get a build from the underlying array
    • Use Len () to get the number of elements, and the cap () to get the capacity
    • In general, use make to create
package mainimport (    "fmt")func main() {    s1 := make([]int, 3, 10) //括号中第一个参数是类型,第二个参数是长度,第三个是容量,如果不设置容量,它就会认为默认是你的长度    fmt.Println(len(s1), cap(s1))}
    • If multiple slice point to the same underlying array, one of the value changes affects all
    • Make ([]t,len,cap)
    • Where the cap can be omitted, then the value of Len is the same
    • Len indicates the number of elements present, and the CAP represents the capacity

Slice relationship to the underlying array

package mainimport (    "fmt")func main() {    a := [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}    sa := a[2:5]    fmt.Println(sa)    sb := a[3:5]    fmt.Println(sb)}

Reslice

    • Reslice indexes are subject to slice slices
    • Index can not exceed the capacity cap () value of slice slices
    • Index out-of-bounds does not cause redistribution of the underlying array but throws an error
package mainimport (    "fmt")func main() {    a := [...]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}    sa := a[2:5]    fmt.Println(sa)    //这里的sa指向的是一个连续的内存块,所以它的最大容量到这个内存块的底部    fmt.Println(len(sa), cap(sa))    sb := sa[1:3]    fmt.Println(sb)    fmt.Println(len(sb), cap(sb))}//PS结果:PS G:\mygo\src\mytest> go run .\myfirst.go[c d e]3 9[d e]2 8

Append

    • You can append elements to the slice tail
    • One slice can be appended to the other slice tail
    • Returns the original slice if the final length does not exceed the capacity appended to the slice
    • If the capacity of the appended slice is exceeded, the array is reassigned and the original data is copied
package mainimport (    "fmt")func main() {    s1 := make([]int, 3, 6)    fmt.Printf("%v, %p \n", s1, s1)    s1 = append(s1, 1, 2, 3)    fmt.Printf("%v, %p \n", s1, s1)    //可以看到两次打印的内存地址是一样的,因为追加的元素没有超出原slice的容量}PS G:\mygo\src\mytest> go run .\myfirst.go[0 0 0], 0xc04203ff50[0 0 0 1 2 3], 0xc04203ff50
package mainimport (    "fmt")func main() {    s1 := make([]int, 3, 6)    fmt.Printf("%v, %p \n", s1, s1)    s1 = append(s1, 1, 2, 3)    fmt.Printf("%v, %p \n", s1, s1)    //可以看到两次打印的内存地址是一样的,因为追加的元素没有超出原slice的容量    s1 = append(s1, 4, 5, 6)    fmt.Printf("%v %p \n", s1, s1)    //此时可以看出,最后一次的打印会重新分配内存地址}PS G:\mygo\src\mytest> go run .\myfirst.go[0 0 0], 0xc04203ff50[0 0 0 1 2 3], 0xc04203ff50[0 0 0 1 2 3 4 5 6] 0xc04203a0c0
package mainimport (    "fmt")func main() {    a := []int{1, 2, 3, 4, 5}    s1 := a[2:5]    s2 := a[1:3]    fmt.Println(s1, s2)    s1[0] = 9    fmt.Println(s1, s2) //slice是指向一个底层的数组,当多个slice指向同一个底层的数组的时候,其中一个发生改变,另外的slice都会发生改变}PS G:\mygo\src\mytest> go run .\myfirst.go[3 4 5] [2 3][9 4 5] [2 9]
package mainimport (    "fmt")func main() {    a := []int{1, 2, 3, 4, 5}    s1 := a[2:5]    s2 := a[1:3]    fmt.Println(s1, s2)    s2 = append(s2, 3, 4, 5, 6, 7, 8, 9, 10) //当append的容量超过底层数组的长度时,会重新分配内存地址并拷贝原始数据    s1[0] = 9    fmt.Println(s1, s2) //slice是指向一个底层的数组,当多个slice指向同一个底层的数组的时候,其中一个发生改变,另外的slice都会发生改变}PS G:\mygo\src\mytest> go run .\myfirst.go[3 4 5] [2 3][9 4 5] [2 3 3 4 5 6 7 8 9 10]

Copy

package mainimport "fmt"func main() {    s1 := []int{1, 2, 3, 4, 5, 6}    s2 := []int{7, 8, 9}    copy(s1, s2) //将S2赋值到s1当中    fmt.Println(s1, s2)}PS G:\mygo\src\mytest> go run .\mysecond.go[7 8 9 4 5 6] [7 8 9]
  Package Mainimport "FMT" Func Main () {s1: = []int{1, 2, 3, 4, 5, 6} s2: = []int{7, 8, 9} s3: = s1[:]/ /complete copy of S1 to S3 FMT. Println (S1, S2) fmt. Println (S3) Fmt. Printf ("%p---%p \ n", S1, S3)}ps g:\mygo\src\mytest> go run. \mysecond.go[1 2 3 4 5 6] [7 8 9][1 2 3 4 5 6]0xc04203ff50- --0XC04203FF50  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.