Golang Arrays and slices

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

Get a better understanding of golang arrays and slices with the following questions

  1. Type

    An array is a value type, and when you assign an array to another array, you pass a copy.
    A slice is a reference type, and an array of tile wrappers is called the underlying array of the slice.
    Let's take a look at the code

    //a是一个数组,注意数组是一个固定长度的,初始化时候必须要指定长度,不指定长度的话就是切片了a := [3]int{1, 2, 3}//b是数组,是a的一份拷贝b := a//c是切片,是引用类型,底层数组是ac := a[:]for i := 0; i < len(a); i++ {  a[i] = a[i] + 1}//改变a的值后,b是a的拷贝,b不变,c是引用,c的值改变fmt.Println(a) //[2,3,4]fmt.Println(b) //[1 2 3]fmt.Println(c) //[2,3,4]
  2. Make
    Make can only be used for slice, map and channel, so the following section of code generates a slice, which is a reference type

    s1 := make([]int, 0, 3)for i := 0; i < cap(s1); i++ {    s1 = append(s1, i)}s2 := s1for i := 0; i < len(a); i++ {    s1[i] = s1[i] + 1}fmt.Println(s1)  //[1 2 3]fmt.Println(s2)  //[1 2 3]
  3. When the slice append exceeds the bounds of the underlying array

    //n1是n2的底层数组n1 := [3]int{1, 2, 3}n2 := n1[0:3]fmt.Println("address of items in n1: ")for i := 0; i < len(n1); i++ {    fmt.Printf("%p\n", &n1[i])}//address of items in n1://0xc20801e160//0xc20801e168//0xc20801e170fmt.Println("address of items in n2: ")for i := 0; i < len(n2); i++ {    fmt.Printf("%p\n", &n2[i])}//address of items in n2://0xc20801e160//0xc20801e168//0xc20801e170//对n2执行append操作后,n2超出了底层数组n1的jn2 = append(n2, 1)fmt.Println("address of items in n1: ")for i := 0; i < len(n1); i++ {    fmt.Printf("%p\n", &n1[i])}//address of items in n1://0xc20801e160//0xc20801e168//0xc20801e170fmt.Println("address of items in n2: ")for i := 0; i < len(n2); i++ {    fmt.Printf("%p\n", &n2[i])}//address of items in n2://0xc20803a2d0//0xc20803a2d8//0xc20803a2e0//0xc20803a2e8
  4. Reference "Invalid"
    Implements a function to delete slice last item

    func rmLast(a []int) {    fmt.Printf("[rmlast] the address of a is %p", a)    a = a[:len(a)-1]    fmt.Printf("[rmlast] after remove, the address of a is %p", a)}

    After calling this function, it is found that the original slice has not changed

    func main() {    xyz := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}    fmt.Printf("[main] the address of xyz is %p\n", xyz)    rmLast(xyz)    fmt.Printf("[main] after remove, the address of xyz is %p\n", xyz)    fmt.Printf("%v", xyz) //[1 2 3 4 5 6 7 8 9]}

    The results are printed as follows:

    [main] the address of xyz is 0xc2080365f0[rmlast] the address of a is 0xc2080365f0[rmlast] after remove, the address of a is 0xc2080365f0[main] after remove, the address of xyz is 0xc2080365f0[1 2 3 4 5 6 7 8 9]

    Here the slice pointer value is printed directly, because slice is a reference type, so the pointer values are the same, we replace the print slice address to see

    func rmLast(a []int) {    fmt.Printf("[rmlast] the address of a is %p", &a)    a = a[:len(a)-1]    fmt.Printf("[rmlast] after remove, the address of a is %p", &a)}func main() {    xyz := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}    fmt.Printf("[main] the address of xyz is %p\n", &xyz)    rmLast(xyz)    fmt.Printf("[main] after remove, the address of xyz is %p\n", &xyz)    fmt.Printf("%v", xyz) //[1 2 3 4 5 6 7 8 9]}

    Results:

    [main] the address of xyz is 0xc20801e1e0[rmlast] the address of a is 0xc20801e200[rmlast] after remove, the address of a is 0xc20801e200[main] after remove, the address of xyz is 0xc20801e1e0[1 2 3 4 5 6 7 8 9]

    This time you can see that slice is actually copying a copy of the slice as a function parameter, because slice itself is a pointer, so from a phenomenon, slice is a reference type.

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.