doucanrui1735 2018-09-18 06:41
浏览 415
已采纳

在golang中打印切片的地址

I am very new on golang. Here I have one question confused me a while. I know we should use "&" to get address, but what if we use printf on slice without "&"?

package main

import "fmt"

var slice = []int{1, 2, 3, 4}

func main() {

   fmt.Printf("address of slice %p 
", slice)

   fmt.Printf("address of slice %p 
", &slice)

}

The result is like this on my MacBook:

address of slice 0x11354d0

address of slice 0x1128be0

Thank you in advance!

  • 写回答

1条回答 默认 最新

  • douliang4858 2018-09-18 06:51
    关注

    Slices in Go are small, struct-like data structures, pointing to a backing array that actually holds the elements of the slice. Slice headers can be modeled with the type reflect.SliceHeader:

    type SliceHeader struct {
            Data uintptr
            Len  int
            Cap  int
    }
    

    Quoting from the package doc of fmt, the verb %p:

    Printing:

    The Verbs:

    Slice:

    %p    address of 0th element in base 16 notation, with leading 0x
    

    Pointer:

    %p    base 16 notation, with leading 0x
    The %b, %d, %o, %x and %X verbs also work with pointers,
    formatting the value exactly as if it were an integer.
    

    So indeed, passing a slice value for the %p verb prints the address of the 0th element, which is the 0th element of the backing array, and when you pass &slice for %p, that will print the address of the slice header (because &slice is already a pointer value).

    To verify, also print explicitly the address of the 0th element:

    fmt.Printf("address of slice %p 
    ", slice)
    fmt.Printf("address of slice %p 
    ", &slice)
    fmt.Println(&slice[0])
    

    Output will be (try it on the Go Playground):

    address of slice 0x170460 
    address of slice 0x17d0b8 
    0x170460
    

    As you can see, &slice[0] is the same value as %p for slice.

    Now let's modify the example to use our own backing array:

    var arr = [4]int{1, 2, 3, 4}
    var slice = arr[:]
    

    So this way we can also print the address of the 0th element of the backing array:

    fmt.Printf("address of slice %p 
    ", slice)
    fmt.Printf("address of slice %p 
    ", &slice)
    fmt.Println(&slice[0])
    fmt.Printf("Array 0th: %p", &arr[0])
    

    Output (try it on the Go Playground):

    address of slice 0x170460 
    address of slice 0x183e78 
    0x170460
    Array 0th: 0x170460
    

    And yes, the address of the 0th element of the backing array is indeed the same as the address of the 0th element of the slice, which is the same value printed for slice when passed for the %p verb.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3