Go学习笔记之:数组
初步: 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() }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。