以下代码展示了用两种方法建立array。
go
package main
import "fmt"
func main() {
var fruitList [4]string
fruitList[0] = "Apple"
fruitList[1] = "Tomato"
fruitList[3] = "Peach"
fmt.Println("Fruit list is: ", fruitList)
fmt.Println("The length of fruit list is: ", len(fruitList))
var vegList = [5]string{"potato", "beans", "mushroom"}
fmt.Println("Vegy list is: ", vegList)
fmt.Println("The length of vegy list is: ", len(vegList))
}
输出为:
Fruit list is: [Apple Tomato Peach]
The length of fruit list is: 4
Vegy list is: [potato beans mushroom ]
The length of vegy list is: 5