Go語言程式:向切片新增元素


在本教程中,我們將學習如何使用不同的示例向切片新增元素。切片就像陣列一樣,是一系列元素的序列。陣列是固定長度的元素序列,而切片是動態陣列,這意味著它的值不是固定的,可以更改。切片比陣列更高效、更快,而且它們是按引用傳遞而不是按值傳遞。讓我們透過示例來理解。

方法 1:使用 append 函式新增字串

在這種方法中,我們將使用 append 函式向切片新增字串元素。我們使用了 append 函式,這是一個內建函式,其功能如下所述。讓我們來看一下它是如何執行的。

語法

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

append 函式用於向陣列切片新增值。它接受多個引數。第一個引數是要向其中新增值的陣列,後跟要新增的值。然後,該函式返回包含所有值的最終陣列切片。

演算法

  • 步驟 1 − 建立一個 main 包,並在程式中宣告 fmt(格式化包),其中 main 生成可執行程式碼,fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 建立一個 main 函式,在該函式中建立一個名為 countries 的切片,並在控制檯上列印它。

  • 步驟 3 − 現在使用 append 函式向切片新增新的字串元素,並將此賦值給名為 new_countries 的變數。

  • 步驟 4 − 使用 fmt.Println() 函式(其中 ln 指的是換行)在控制檯上列印 new_countries 中儲存的值。

示例

使用 append 函式向切片新增字串元素的 Go 語言程式

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 

Go 語言中的 copy 函式用於將一個源陣列的值複製到目標陣列,並返回複製的元素數量作為結果。它以兩個陣列作為引數。

演算法

  • 步驟 1 − 建立一個 main 包,並在程式中宣告 fmt(格式化包),其中 main 生成可執行程式碼,fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 建立一個 main 函式,在該函式中建立一個名為 countries 的切片,並在控制檯上列印它。

  • 步驟 3 − 建立另一個字串型別的切片,並將它的內容複製到舊切片。

  • 步驟 4 − 使用 fmt.Println() 函式在螢幕上列印元素複製後舊切片的內容。

示例

使用 copy 函式向切片新增元素的 Go 語言程式

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:使用 append 函式新增整數值

在這種方法中,我們將使用 append 函式向切片新增整數元素。我們使用了 append 函式,這是一個內建函式,其功能如下所述。讓我們來看一下它是如何執行的。

語法

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

append 函式用於向陣列切片新增值。它接受多個引數。第一個引數是要向其中新增值的陣列,後跟要新增的值。然後,該函式返回包含所有值的最終陣列切片。

演算法

  • 步驟 1 − 建立一個 main 包,並在程式中宣告 fmt(格式化包),其中 main 生成可執行程式碼,fmt 幫助格式化輸入和輸出。

  • 步驟 2 − 建立一個 main 函式,在該函式中建立一個名為 numbers 的切片,並在控制檯上列印它。

  • 步驟 3 − 現在使用 append 函式向切片新增新的整數元素,並將此賦值給名為 new_numbers 的變數。

  • 步驟 4 − 使用 fmt.Println() 函式(其中 ln 指的是換行)在控制檯上列印 new_numbers 中儲存的值。

示例

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 函式新增整數元素。這兩個示例給出類似的輸出。因此,程式成功執行。

更新於:2023年1月17日

瀏覽量 1K+

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告