数组【进阶】
2024-07-17 本文已影响0人
该死的金箍
package main
import "fmt"
func main() {
////一维数组
//var a [5]int = [5]int{1, 2, 3, 4, 5}
////for循环打印
//for i := 0; i < len(a); i++ {
// fmt.Println(a[i])
//}
//b := [5]string{"A", "B", "C", "D", "E"}
//bb := [...]string{"A", "B", "C", "D", "E"}
//c := [...]string{"one", "two", "three", "four", "five"}
//balance := [5]float32{1: 2.0, 3: 7.10} //指定下标 创建 其它下标会默认值进行赋值 [0 2 0 7 0]
//fmt.Println(a)
//fmt.Println(b)
//fmt.Printf("%#v\n", b == bb) //true
//fmt.Printf("%#v\n", c) //程序计算出数组长度 [5]string{"one", "two", "three", "four", "five"}
//fmt.Println(balance) //[0 2 0 7.1 0]
//balance[0] = 1
////balance[5] = 5 //会报错 超出数据长度 invalid argument: index 5 out of bounds [0:5]
//fmt.Println(balance) //[1 2 0 7.1 0]
//fmt.Println(len(c)) //数组长度 5
//d := [...]struct {
// name string
// age uint8
//}{
// {"user1", 10}, // 可省略元素类型。
// {"user2", 20}, // 别忘了最后一行的逗号。
//}
//fmt.Printf("%#v\n", d)
////二位数组
//var aa [3][3]int = [3][3]int{{1, 2, 3}, {2, 3, 4}, {3, 4, 5}}
//for _, value := range aa {
// fmt.Println(value)
// for _, v := range value {
// fmt.Println(v)
// }
//}
//fmt.Printf("%#v\n", aa)
//var bb [2][2]string = [...][2]string{{"A", "a"}, {"B", "b"}}
//bb := [...][2]string{{"A", "a"}, {"B", "b"}}
//fmt.Print(aa, bb)
cc := [2]int{}
fmt.Printf("a: %p\n", &cc) //a: 0xc000096080
test(cc)
fmt.Println("a:", cc) //a: [0 0]
a := [...]int{1, 2, 3}
println(len(a), cap(a)) //3 3
}
func test(x [2]int) {
fmt.Printf("x: %p\n", &x) //x: 0xc0000960b0
x[1] = 1000
fmt.Println("x:", x) //x: [0 1000]
}