golang 数组、指针数组、数组指针使用总结

package main

import (
	"fmt"

)

func main() {
	//创建
	at:=[6]int{}
	
	//下标赋值
	ap:=[5]int{3:2}//下标3的值赋值为2
	fmt.Println("ap[3]:", ap[3])
	
	//遍历1
	as := [3]int{1, 2, 3}
	for i, p := range as {
        fmt.Println(i, "---", p)
        at[i] = as[i]
        fmt.Println(i, "---", at[i])
    }
    
    //遍历2
    le:=len(as)
    for i:=0;i<le;i++{
        fmt.Println(as[i])
    }
    
    //指针数组
    av := [3]int{1, 2, 3}
    var pa [3]*int
    for i, p := range av {
        fmt.Println(i, "---", p)
        pa[i] = &av[i] //pa[i]为地址
        fmt.Println(i, "---", *(pa[i]))
        fmt.Println(i, "---", pa[i])
    }
    
    //数组指针
    var pa2 *[3]int
    pa2 = &as
    fmt.Println(*pa2)
    
    fmt.Println(cap(*pa2))
}

结果

ap[3]: 2

0 — 1

0 — 1

1 — 2

1 — 2

2 — 3

2 — 3

1

2

3

0 — 1

0 — 1

0 — 0xc00011e160

1 — 2

1 — 2

1 — 0xc00011e168

2 — 3

2 — 3

2 — 0xc00011e170

[1 2 3]

3

    原文作者:墨痕诉清风
    原文地址: https://blog.csdn.net/u012206617/article/details/105905576
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞