golang中数组和切片用的比较多,下面给个demo看看二维的数组和二维切片。
package main
import (
"fmt"
)
func printarray(a [2][2]string) {
for _, v1 := range a {
for _, v2 := range v1 {
fmt.Printf("%s ", v2)
}
fmt.Printf("\n")
}
}
func main() {
//数组长度固定,数组是值类型
a := [2][2]string{
{"00", "01"},
{"10", "11"},
}
printarray(a)
var b [2][2]string
b[0][0] = "00"
b[0][1] = "01"
b[1][0] = "10"
b[1][1] = "11"
fmt.Printf("\n")
printarray(b)
//不固定大小
pls := [][]string {
{"00", "01"},
{"10","11","12"},
}
for _, v1 := range pls {
for _, v2 := range v1 {
fmt.Printf("%s ", v2)
}
fmt.Printf("\n")
}
fmt.Println(cap(pls))
}