Go語言程式將一個數組推入另一個數組


在本教程中,我們將編寫一個 Go 語言程式,將一個數組推入另一個數組。有多種方法可以實現這一點。最簡單的方法是使用相等運算子。在這裡,我們將看到三種方法,可以透過這些方法將一個數組的內容傳輸到 Go 程式語言中的另一個數組。

方法 1:使用相等運算子將字串陣列推入空陣列

在這種方法中,我們將使用相等運算子在程式的 main() 部分將一個數組的內容新增到另一個數組。請注意,透過這種方法傳輸元素時,新陣列的元素將被覆蓋。

語法

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

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

func make ([] type, size, capacity)

Go 語言中的 make 函式用於建立陣列/對映,它接受要建立的變數的型別、大小和容量作為引數,並返回可以儲存在變數中的切片。

演算法

步驟 1 − 首先,我們需要匯入 fmt 包。

步驟 2 − 現在,開始 main() 函式。在此函式內部,使用 make() 函式建立兩個字串資料型別的陣列,並在其中一個數組中儲存資料。

步驟 3 − 然後,使用 fmt.Println() 函式在螢幕上列印這些陣列。之後,使用相等 (==) 運算子將第一個陣列的內容複製到第二個陣列。

步驟 4 − 複製陣列後,在螢幕上列印新形成的陣列。

示例

使用相等運算子將字串陣列推入空陣列的 Go 語言程式。

package main
import "fmt"
func main() {
   
   // initializing an array
   array := make([]string, 0, 3)
   array1 := make([]string, 0, 3)
   array = append(array, "Apple", "Mango", "Banana")
   fmt.Println("The first array is:", array)
   fmt.Println("The second array is:", array1)
   
   // copying array contents
   array1 = array
   fmt.Println()
   
   // printing the array
   fmt.Println("The array obtained after copying the contents is:", array1)
}

輸出

The first array is: [Apple Mango Banana]
The second array is: []
The array obtained after copying the contents is: [Apple Mango Banana]

方法 2:使用 Copy() 函式將字串陣列推入另一個數組

在這種方法中,我們將使用 Go 中的 copy() 庫函式將一個數組的內容推入另一個數組。Copy 是 Go 中字串包中的一個內建函式。

語法

func copy(dst, str[] type) int

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

演算法

步驟 1 − 匯入 fmt 包。

步驟 2 − 開始 main() 函式。在此函式內部,初始化兩個字串資料型別的陣列,並使用 append() 函式向其中儲存值。

步驟 3 − 現在,在螢幕上列印這兩個陣列,並使用 copy 函式將第一個陣列的內容複製到第二個陣列。

步驟 4 − 複製元素後,將複製的元素數量儲存在一個名為 result 的變數中,並在螢幕上列印最終陣列以及複製的元素數量。

示例

使用 copy() 函式將字串陣列推入另一個數組的 Go 語言程式

package main
import "fmt"
func main() {
   
   // initializing an array
   array := make([]string, 0, 3)
   var array1 []string
   array = append(array, "Apple", "Mango", "Banana")
   array1 = append(array1, "pine-apple", "Cherry")
   fmt.Println("The first array is:", array)
   fmt.Println("The second array is:", array1)
   
   // copying array contents
   result := copy(array1, array)
   fmt.Println()
   
   // printing the array
   fmt.Println("The array obtained after copying the contents is:", array1)
   fmt.Println("The number of elements copied are:", result)
}

輸出

The first array is: [Apple Mango Banana]
The second array is: [pine-apple Cherry]
The array obtained after copying the contents is: [Apple Mango]
The number of elements copied are: 2

方法 3:使用 Append() 函式將一個數組推入另一個數組

在這種方法中,我們將編寫一個 Go 語言程式,使用 append() 函式將一個數組的元素推入另一個數組。當我們使用此函式將一個數組的內容複製到另一個數組時,新陣列的當前元素不會被覆蓋。

演算法

步驟 1 − 匯入 fmt 包。

步驟 2 − 啟動 main() 函式並初始化兩個陣列。在兩個陣列中儲存資料,並使用 fmt.Println() 函式在螢幕上列印它們。

步驟 3 − 然後,使用 append 函式將第一個陣列的內容複製到第二個陣列。函式的第一個引數是要複製值的陣列。

步驟 4 − 將陣列內容複製到新陣列後,在螢幕上列印新形成的陣列。

示例

使用 append() 函式將一個數組推入另一個數組的 Go 語言程式。

package main
import "fmt"
func main() {
   
   // initializing an array
   array := make([]string, 0, 3)
   var array1 []string
   array = []string{"Apple", "Mango", "Banana"}
   array1 = append(array1, "pine-apple", "Cherry")
   fmt.Println("The first array is:", array)
   fmt.Println("The second array is:", array1)

   // copying array contents
   array1 = append(array1, array...)
   fmt.Println()
   
   // printing the array
   fmt.Println("The array obtained after copying the contents is:", array1)
}

輸出

The first array is: [Apple Mango Banana]
The second array is: [pine-apple Cherry]
The array obtained after copying the contents is: [pine-apple Cherry Apple Mango Banana]

結論

我們已經成功編譯並執行了一個 Go 語言程式,用於將一個數組的內容複製到另一個數組,並提供了示例。我們在這裡製作了三個程式,在第一個程式中,我們只是簡單地使用相等運算子來儲存值,而在第二個和第三個示例中,我們使用了 Go 庫函式來實現結果。

更新於: 2023年1月10日

3K+ 瀏覽量

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告