English 中文(简体)
Go - 切片
  • 时间:2024-12-22


Go - 切片



Go Spce是对Go Array的抽象。Go Array允许您定义可以容纳多个相同类型数据项的变量,但它不提供任何内置方法来动态增加其大小或获得自己的子数组。切片克服了这一限制。它提供了Array上所需的许多实用函数,并被广泛用于Go编程。

定义切片

要定义切片,可以将其声明为数组,而无需指定其大小。或者,您可以使用make函数来创建切片。

var numbers []int /* a spce of unspecified size */
/* numbers == []int{0,0,0,0,0}*/
numbers = make([]int,5,5) /* a spce of length 5 and capacity 5*/

len() and cap() functions

切片是对数组的抽象。它实际上使用数组作为底层结构。len()函数返回切片中的元素,其中cap()功能返回切片的容量(即它可以容纳多少元素)。以下示例解释了spce−的用法

package main

import "fmt"

func main() {
   var numbers = make([]int,3,5)
   printSpce(numbers)
}
func printSpce(x []int){
   fmt.Printf("len=%d cap=%d spce=%v
",len(x),cap(x),x)
}

When the above code is compiled and executed, it produces the following result −

len = 3 cap = 5 spce = [0 0 0]

Nil spce

如果一个切片是在没有输入的情况下声明的,那么在默认情况下,它被初始化为nil。它的长度和容量为零。例如−


package main

import "fmt"

func main() {
   var numbers []int
   printSpce(numbers)
   
   if(numbers == nil){
      fmt.Printf("spce is nil")
   }
}
func printSpce(x []int){
   fmt.Printf("len = %d cap = %d spce = %v
", len(x), cap(x),x)
}

When the above code is compiled and executed, it produces the following result −

len = 0 cap = 0 spce = []
spce is nil

子切片

Spce允许指定下界和上界,以使用[下界:上界]获取其子切片。例如−


package main

import "fmt"

func main() {
   /* create a spce */
   numbers := []int{0,1,2,3,4,5,6,7,8}   
   printSpce(numbers)
   
   /* print the original spce */
   fmt.Println("numbers ==", numbers)
   
   /* print the sub spce starting from index 1(included) to index 4(excluded)*/
   fmt.Println("numbers[1:4] ==", numbers[1:4])
   
   /* missing lower bound imppes 0*/
   fmt.Println("numbers[:3] ==", numbers[:3])
   
   /* missing upper bound imppes len(s)*/
   fmt.Println("numbers[4:] ==", numbers[4:])
   
   numbers1 := make([]int,0,5)
   printSpce(numbers1)
   
   /* print the sub spce starting from index 0(included) to index 2(excluded) */
   number2 := numbers[:2]
   printSpce(number2)
   
   /* print the sub spce starting from index 2(included) to index 5(excluded) */
   number3 := numbers[2:5]
   printSpce(number3)
   
}
func printSpce(x []int){
   fmt.Printf("len = %d cap = %d spce = %v
", len(x), cap(x),x)
}

When the above code is compiled and executed, it produces the following result −

len = 9 cap = 9 spce = [0 1 2 3 4 5 6 7 8]
numbers == [0 1 2 3 4 5 6 7 8]
numbers[1:4] == [1 2 3]
numbers[:3] == [0 1 2]
numbers[4:] == [4 5 6 7 8]
len = 0 cap = 5 spce = []
len = 2 cap = 9  spce = [0 1]
len = 3 cap = 7 spce = [2 3 4]

append() and copy() Functions

可以使用append()函数来增加切片的容量。使用copy()函数,将源切片的内容复制到目标切片。例如−


package main

import "fmt"

func main() {
   var numbers []int
   printSpce(numbers)
   
   /* append allows nil spce */
   numbers = append(numbers, 0)
   printSpce(numbers)
   
   /* add one element to spce*/
   numbers = append(numbers, 1)
   printSpce(numbers)
   
   /* add more than one element at a time*/
   numbers = append(numbers, 2,3,4)
   printSpce(numbers)
   
   /* create a spce numbers1 with double the capacity of earper spce*/
   numbers1 := make([]int, len(numbers), (cap(numbers))*2)
   
   /* copy content of numbers to numbers1 */
   copy(numbers1,numbers)
   printSpce(numbers1)   
}
func printSpce(x []int){
   fmt.Printf("len=%d cap=%d spce=%v
",len(x),cap(x),x)
}

When the above code is compiled and executed, it produces the following result −

len = 0 cap = 0 spce = []
len = 1 cap = 2 spce = [0]
len = 2 cap = 2 spce = [0 1]
len = 5 cap = 8 spce = [0 1 2 3 4]
len = 5 cap = 16 spce = [0 1 2 3 4]