刚接触go时间不长,关于array与slice做一个笔记
eg:
package main
import "fmt"
func main() {
var arr1 [5]int
arr2 := [3]int{1, 3, 5}
arr3 := [...]int{2, 4, 6, 8, 10}
var grid [4][5]int
fmt.Println(arr1, arr2, arr3)
fmt.Println(grid)
printArray(arr1)
printArray(arr3)
fmt.Println(arr1, arr2, arr3)
fmt.Println(grid)
}
func printArray(ints [5]int) {
ints[0] = 100
for i, v := range ints {
fmt.Println(i, v)
}
}
eg2:
package main
import "fmt"
func main() {
var arr1 [5]int
arr2 := [3]int{1, 3, 5}
arr3 := [...]int{2, 4, 6, 8, 10}
var grid [4][5]int
fmt.Println(arr1, arr2, arr3)
fmt.Println(grid)
printArray(arr1[:])
printArray(arr3[:])
fmt.Println(arr1, arr2, arr3)
fmt.Println(grid)
}
func printArray(ints []int) {
ints[0] = 100
for i, v := range ints {
fmt.Println(i, v)
}
}