Golang 如何附加切片

Golang 如何附加切片

在Go语言中,切片比数组更强大、灵活、方便,而且是一种轻量级数据结构。切片是一个可变长度序列,存储相似类型的元素,在同一切片中不允许存储不同类型的元素。它就像一个有索引值和长度的数组,但是切片的大小是可调整的,它们不是像数组那样固定大小。

我们已经知道切片是动态的,因此可以通过append()函数将新元素附加到切片上。这个函数将新元素附加到切片的末尾。

语法:

func append(s []T, x ...T) []T

在这里,此函数采用s切片和x…T,这意味着此函数采用x参数的可变数量的参数。这种类型的函数也称为可变函数。

如果切片由数组支持而数组长度固定,那么切片如何具有动态长度呢?

好吧,答案是当将新元素附加到切片时,会创建一个新的数组。 现在,将存在的数组中的元素复制到新数组中,并返回对该数组(新数组)的新切片引用。 因此,由于这一点,切片的容量将从旧容量加倍(如示例1所示)。但是,如果现有切片具有足够的容量来容纳新元素,则不会创建新数组,并且元素将存储在现有的基础数组中(如示例2所示)。

例1:

// Go program to illustrate the
// concept of appending slices.
package main
  
import (
    "fmt"
)
  
func main() {
  
    // Creating and initializing slice
    // Using shorthand declaration
    s1 := []int{234, 567, 7890, 1234, 234}
    s2 := []string{"Geeks", "For", "Geeks"}
  
    // Displaying slices with
    // their length and capacity
    fmt.Println("Slice_1: ", s1)
    fmt.Println("Length of Slice_1: ", len(s1))
    fmt.Println("Capacity of Slice_1: ", cap(s1))
    fmt.Println()
    fmt.Println("Slice_2: ", s2)
    fmt.Println("Length of Slice_2: ", len(s2))
    fmt.Println("Capacity of Slice_2: ", cap(s2))
  
    // Appending slices
    // Using append() function
    res1 := append(s1, 1000)
    res2 := append(s2, "GFG")
  
    // Displaying results
    fmt.Println()
    fmt.Println("New slice_1: ", res1)
    fmt.Println("New length of slice_1: ", len(res1))
    fmt.Println("New capacity of slice_1: ", cap(res1))
    fmt.Println()
    fmt.Println("New slice_2: ", res2)
    fmt.Println("New length of slice_2: ", len(res2))
    fmt.Println("New capacity of slice_2: ", cap(res2))
} 

输出:

Slice_1:  [234 567 7890 1234 234]
Length of Slice_1:  5
Capacity of Slice_1:  5

Slice_2:  [Geeks For Geeks]
Length of Slice_2:  3
Capacity of Slice_2:  3

New slice_1: [234 567 7890 1234 234 1000]
New length of slice_1:  6
New capacity of slice_1:  12

New slice_2:  [Geeks For Geeks GFG]
New length of slice_2:  4
New capacity of slice_2:  6

例2:

// Go程序来说明
// 切片附加的概念。
package main
  
import "fmt"
  
func main() {
  
    // 使用make()函数创建和初始化切片
    // 这里4和6是切片的长度和容量
    s1 := make([]int, 4, 6)
  
    // 使用copy()函数将元素复制到切片
    copy(s1, []int{123, 456, 789, 977})
  
    // 显示切片
    fmt.Println("Slice : ", s1)
    fmt.Println("Length: ", len(s1))
    fmt.Println("Capacity: ", cap(s1))
  
    // 使用append()函数附加切片
    s2 := append(s1, 3454, 678)
  
    // 显示切片
    fmt.Println()
    fmt.Println("Slice : ", s2)
    fmt.Println("Length: ", len(s2))
    fmt.Println("Capacity: ", cap(s2))
  
} 

输出:

Slice :  [123 456 789 977]
Length:  4
Capacity:  6

Slice :  [123 456 789 977 3454 678]
Length:  6
Capacity:  6

附加到零切片: 我们知道,零值切片类型是nil,并且这种类型的切片的容量和长度为0。但是可以通过append函数将值附加到nil切片。

例子:

// Go程序来说明
// 切片附加到nil的概念。
package main
  
import "fmt"
  
func main() {
  
    // 创建nil切片
    var s1 []int
  
    // 显示切片
    fmt.Println("Slice : ", s1)
    fmt.Println("Length: ", len(s1))
    fmt.Println("Capacity: ", cap(s1))
  
    // 使用append()函数附加到nil切片
    s2 := append(s1, 89, 45, 67, 23)
  
    // 显示切片
    fmt.Println()
    fmt.Println("Slice : ", s2)
    fmt.Println("Length: ", len(s2))
    fmt.Println("Capacity: ", cap(s2))
  
} 

输出:

Slice :  []
Length:  0
Capacity:  0

Slice :  [89 45 67 23]
Length:  4
Capacity:  4

使用…操作符附加到另一个切片: 可以使用 操作符将一个切片附加到另一个切片。

例子:

// Go程序来说明概念 
// 附加到另一个切片。
package main
  
import "fmt"
  
func main() {
  
    // 使用简短的声明创建和初始化切片
    s1 := []int{234, 567, 7890, 1234, 234}
    s2 := []int{10, 100, 1000, 10000}
  
    // 显示它们的长度和容量
    fmt.Println("Slice_1: ", s1)
    fmt.Println("Slice_1长度:", len(s1))
    fmt.Println("Slice_1容量:", cap(s1))
      
        fmt.Println()
      
    fmt.Println("Slice_2: ", s2)
    fmt.Println("Slice_2长度:", len(s2))
    fmt.Println("Slice_2容量:", cap(s2))
  
    // 使用append()函数附加到另一个切片
    res1 := append(s1, s2...)
  
    // 显示结果
    fmt.Println()
    fmt.Println("新的Slice_1:", res1)
    fmt.Println("新的Slice_1长度:", len(res1))
    fmt.Println("新的Slice_1容量:", cap(res1))
  
} 

输出:

Slice_1:  [234 567 7890 1234 234]
Slice_1长度: 5
Slice_1容量: 5

Slice_2:  [10 100 1000 10000]
Slice_2长度: 4
Slice_2容量: 4

新的Slice_1: [234 567 7890 1234 234 10 100 1000 10000]
新的Slice_1长度: 9
新的Slice_1容量: 12

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程