How to print the memory address of a slice in Golang?

前端 未结 3 502
长情又很酷
长情又很酷 2021-02-04 01:34

I have some experience in C and I am totally new to golang.

func learnArraySlice() {
  intarr := [5]int{12, 34, 55, 66, 43}
  slice := intarr[:]
  fmt.Printf(\"t         


        
3条回答
  •  一生所求
    2021-02-04 02:21

    For the addresses of the slice underlying array and the array (they are the same in your example),

    package main
    
    import "fmt"
    
    func main() {
        intarr := [5]int{12, 34, 55, 66, 43}
        slice := intarr[:]
        fmt.Printf("the len is %d and cap is %d \n", len(slice), cap(slice))
        fmt.Printf("address of slice %p add of Arr %p\n", &slice[0], &intarr)
    }
    

    Output:

    the len is 5 and cap is 5 
    address of slice 0x1052f2c0 add of Arr 0x1052f2c0
    

提交回复
热议问题