go for循环和数组的熟练使用

package main

import "fmt"

func fab(n int) {
	var a []uint64
	a = make([]uint64, n)

	a[0] = 1
	a[1] = 1

	for i := 2; i < n; i++ {
		a[i] = a[i-1] + a[i-2]
	}

	for _, v := range a {
		fmt.Println(v)
	}
}

func testArray2() {
	var a [2][5]int = [...][5]int{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}

	for row, v := range a {
		for col, v1 := range v {
			fmt.Printf("(%d,%d)=%d ", row, col, v1)
		}
		fmt.Println()
	}
}

func main() {
	testArray2()
	fab(10)
}

打印结果:

(0,0)=1 (0,1)=2 (0,2)=3 (0,3)=4 (0,4)=5
(1,0)=6 (1,1)=7 (1,2)=8 (1,3)=9 (1,4)=10
1
1
2
3
5
8
13
21
34
55

你可能感兴趣的:(go)