2015-06-07 203 views

回答

0

您可以聲明一個相同類型的新數組,並通過賦值來複制這些值。

// Declare a string array of five elements. 
var array1 [5]string 

// Declare a second string array of five elements. 
// Initialise the array with values. 
array2 := [5]string{"A", "B", "C", "D", "E"} 

// Copy the values from array2 into array1. 
array1 = array2 

請注意,你不能分配在不同大小的陣列:

// Declare a string array of four elements. 
var array1 [4]string 

// Declare a second string array of five elements. 
// Initialize the array with colors. 
array2 := [5]string{"A", "B", "C", "D", "E"} 

// Copy the values from array2 into array1. 
array1 = array2 

Compiler Error: 
cannot use array2 (type [5]string) as type [4]string in assignment 
+2

['copy(array1 [:],array2 [:])'](https://play.golang.org/p/c5Iwq0fpmT) –

+0

正確的你可以複製但不能分配。 – sergserg

相關問題