MENU

Go 学习笔记:切片slice

August 23, 2021 • Read: 262 • Golang

在Go中,因为数组的长度是不可变的,这就会导致在某些场景中的尴尬场面。因此,go提供了一种“动态数组”——slice,这种类型长度不固定,可以扩容。具体可以参考Python的”列表“,不过他俩还是区别蛮大的

首先我们可以创建一个切片

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-23 17:06:39
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    slice1 := make([]int, 20, 30) 
    // 类型、切片长度、预留长度(可选)
    fmt.Println(slice1)
    fmt.Printf("%T\n", slice1) // []int
}

make 函数是用来为slice map channel 分配内存的。当然,除了make函数以外,还有其他的方式

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-23 17:27:08
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    slice1 := []int{1, 2, 3}
    fmt.Println(slice1)
    fmt.Printf("%T\n", slice1) // []int
}

切片也可以利用数组初始化

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-23 17:31:58
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    array := [5]int{1, 2, 3, 4, 5}
    slice1 := array[:]
    fmt.Println(slice1)
    fmt.Println(array)
    fmt.Printf("%T\n", slice1)
    fmt.Printf("%T\n", array)
}

我们也可以不引用完

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-23 17:32:42
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    array := [5]int{1, 2, 3, 4, 5}
    slice1 := array[0:2] // [1 2], 取左闭右开的一个区间
    fmt.Println(slice1)
    fmt.Println(array)
    fmt.Printf("%T\n", slice1)
    fmt.Printf("%T\n", array)
}

当然,go提供了两个函数,用来获取slice的长度与切片最大长度

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-23 17:44:12
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    slice := make([]int, 5, 10)
    fmt.Println(len(slice), cap(slice)) // 5, 10
}

slice也可以像Python的列表一样切片

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-23 17:50:00
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
    fmt.Println(slice[0:5])
    fmt.Println(slice[:])
    fmt.Println(slice[:5])
    fmt.Println(slice[2:])
}

如果想要拓展切片的空间,可以使用appendcopy函数

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-23 17:52:00
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
    fmt.Println(len(slice), cap(slice))
    slice = append(slice, 11) // 在最后面追加元素
    fmt.Println(len(slice), cap(slice)) 
    // 追加后,预留的空间翻倍了
    fmt.Println(slice)
}

copy函数可以帮助你将一个小的切片复制到另一个大的切片中去

/*
 * @Author: NorthCityChen
 * @LastEditTime: 2021-08-23 17:56:58
 * @Description:
 * @Website: https://grimoire.cn
 * Copyright (c) NorthCityChen All rights reserved.
 */
package main

import (
    "fmt"
)

func main() {
    slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
    slice1 := make([]int, 11, 22)
    copy(slice1, slice)
    fmt.Println(slice, cap(slice))
    fmt.Println(slice1, cap(slice1))

    fmt.Println(slice)
    fmt.Println(slice1)
}
Archives Tip
QR Code for this page
Tipping QR Code