我有一个字符串元素数组,我希望将这些值复制到一个新的数组中。我可以怎么做?
You can declare a new array of the same type and copy over the values by assigning it.
// 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
Keep in mind that you cannot assign over an array of different size:
// 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