Golang 向切片(slice)中添加元素

Golang 向切片(slice)中添加元素

在本教程中,我们将学习如何使用不同的示例向切片中添加元素。切片就像数组一样,是一系列元素的序列。数组是一个固定的元素序列,而切片是一个动态数组,意味着它的值是不固定的,可以改变的。相较于数组,切片更高效、更快,并且是按引用传递而不是按值传递。让我们通过示例来了解一下。

方法1:使用append函数与字符串

在这种方法中,我们将使用append函数将字符串元素添加到切片中。我们使用了一个append函数,它是一个内置函数,其功能如下所述。让我们来看看它是如何执行的。

语法

func append(slice, element_1, element_2…, element_N) []T

append函数用于向数组切片中添加值。它接受多个参数。第一个参数是要添加值的数组,后面是要添加的值。该函数返回包含所有值的最终数组切片。

步骤

  • 步骤1 - 创建一个main包,并在其中声明fmt(format包)包。main包用于生成可执行代码,fmt包用于格式化输入和输出。

  • 步骤2 - 创建一个main函数,在该函数中创建一个名为countries的切片,并在控制台上打印它。

  • 步骤3 - 现在使用append函数将新的字符串元素添加到切片中,并将其赋值给名为new_countries的变量。

  • 步骤4 - 使用fmt.Println()函数在控制台上打印存储在new_countries中的值,其中ln表示换行。

示例

使用append函数将元素添加到切片的Golang程序,其中元素为字符串。

package main
import "fmt"
func main() {

   // create a slice of type string
   Countries := []string{"Iceland", "Switzerland"}
   fmt.Println("The slice before adding of elements is:", Countries)

   // append new elements and old slice in new slice
   new_countries := append(Countries, "Canada", "Italy")
   fmt.Println("The new slice after adding elements is:")
   fmt.Println(new_countries) // print new slice
}

输出

The slice before adding of elements is: [Iceland Switzerland]
The new slice after adding elements is:
[Iceland Switzerland Canada Italy]

方法2:使用copy函数

语法

在这个方法中,我们将使用copy函数来向切片中添加元素。copy函数是一个内置函数,其特性如下所述。让我们学习执行程序所需的步骤。

func copy(dst, str[] type) int

copy 函数在Go语言中用于将一个源数组的值复制到目标数组,并将复制的元素个数作为结果返回。它以两个数组作为参数。

步骤

  • 步骤1 - 创建main包并在程序中声明fmt(格式化包)包,其中main生成可执行代码,fmt用于格式化输入和输出。

  • 步骤2 - 创建main函数,并在该函数中创建一个名为countries的切片,并将其打印到控制台上。

  • 步骤3 - 创建另一个字符串类型的切片,并将其内容复制到旧切片中。

  • 步骤4 - 使用fmt.Println()函数在屏幕上打印元素复制后的旧切片的内容。

示例

Golang程序使用copy函数向切片中添加元素

package main
import "fmt"
func main() {

   // create a slice of type string
   countries := []string{"Canada", "Italy"}
   fmt.Println("The slice before adding element is:", countries)
   morecountries := []string{"Finland", "Switzerland"}

   // create a new slice to copy the elements of slice
   new_slice := copy(countries, morecountries)
   fmt.Println("The slice after adding element in slice is:")
   fmt.Println("The new slice we created has", new_slice, "elements->", countries)
}

输出

The slice before adding element is: [Canada Italy]
The slice after adding element in slice is:
The new slice we created has 2 elements-> [Finland Switzerland]

方法3:使用附加函数添加整数值

在这种方法中,我们将使用附加函数将整数元素添加到切片中。我们使用了一个附加函数,该函数是一个内置函数,其特点如下所述。让我们来看看它的语法和执行方式。

语法

func append(slice, element_1, element_2…, element_N) []T

append函数用于向数组切片添加值。它接受多个参数。第一个参数是我们希望添加值的数组,后面是要添加的值。函数然后返回包含所有值的最终数组切片。

步骤

  • 步骤1 - 创建一个main package,并在其中声明fmt(format包)。主要负责生成可执行代码,fmt负责格式化输入和输出。

  • 步骤2 - 创建一个main函数,在该函数中使用名为numbers的切片,并将其打印在控制台上。

  • 步骤3 - 现在使用append函数将新的整数元素添加到切片中,并将其赋值给名为new_numbers的变量。

  • 步骤4 - 使用fmt.Println()函数将存储在new_numbers中的值打印在控制台上,其中ln表示换行。

示例

package main
import "fmt"
func main() {

   // create a slice of type int
   numbers := []int{1, 2}
   fmt.Println("The slice before adding of elements is:", numbers)

   // append new elements in the slice
   new_numbers := append(numbers, 3, 4) 
   fmt.Println("The new slice after adding elements is:")

   // print new slice
   fmt.Println(new_numbers)  
}

输出

The slice before adding of elements is: [1 2]
The new slice after adding elements is:
[1 2 3 4]

结论

我们执行了将元素添加到切片的程序,使用了三个示例。在第一个示例中,我们使用了append函数来添加字符串元素,在第二个示例中,我们使用了copy函数来添加值,在第三个示例中,我们使用append函数来添加整数元素。这两个示例都给出了相似的输出。因此,程序执行成功。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程