初步: array_example.go
package main import ( "fmt" ) func main() { var arr_1 [10]int var sum int fmt.Println("\nlen(arr_1): ", len(arr_1)) fmt.Println("initial value of arr_1:") for _, elem := range arr_1 { fmt.Print(elem, " ") } fmt.Println("\ninitial value of sum: ", sum, "\n") fmt.Println("arr_1:") for i := 0; i < len(arr_1); i++ { arr_1[i] = len(arr_1) - 1 - i fmt.Println(arr_1[i]) } fmt.Println("\neach set of range arr_1:\nidx val\n-------") for idx, val := range arr_1 { fmt.Println(idx, val) } for _, elem := range arr_1 { sum += elem } fmt.Print("\nsum of arr_1: ", sum, "\n\n") // define array with length unknown, must initialize when defining var arr_2 = [...]string{ "Sun", "Air", "Water"} fmt.Println("arr_2:") for _, elem := range arr_2 { fmt.Println(elem) } fmt.Println() }
有疑问加站长微信联系(非本文作者)