Address of slice in golang

huangapple go评论81阅读模式
英文:

Address of slice in golang

问题

我有这段代码:

package main

import (
    "fmt"
)

func Extend(slice []int, element int) []int {
    n := len(slice)
    if n == cap(slice) {
        // Slice is full; must grow.
        // We double its size and add 1, so if the size is zero we still grow.
        newSlice := make([]int, len(slice), 2*len(slice)+1)
        copy(newSlice, slice)
        slice = newSlice
    }
    slice = slice[0 : n+1]
    slice[n] = element
    return slice
}

func main() {
    slice := make([]int, 0, 5)
    for i := 0; i < 10; i++ {
        slice = Extend(slice, i)
        fmt.Printf("len=%d cap=%d slice=%v\n", len(slice), cap(slice), slice)
        fmt.Println("address of 0th element:", &slice[0])
        fmt.Println("address of slice:", &slice) // 为什么这里打印的是切片而不是它的地址?
        fmt.Printf("address of slice: %p\n", &slice) // 为什么这个与下面的不同?为什么当创建一个指向不同数组的新切片时,它不会改变?
        fmt.Printf("address of slice: %p\n\n", slice)
    }
}

我的问题是关于循环中底部的第二个 Println。如果你运行它,你会看到它打印出 &[values...]。为什么它不打印出地址?我知道你可以使用 Printf 等其他方法来实现,而且它可以正常工作,但是 Println 呢?Println 与 &slice[0] 一起使用时可以正常工作,它打印出地址而不是值,但是 Println 与 &slice 一起使用时就不行了。

我还注意到,当我使用 %p 格式化符号打印 &slice 时,与仅打印 slice 时,我得到的地址是不同的。为什么?而且,当更改它时(运行代码,程序会调整数组的大小并创建一个指向不同数组的新切片),&slice 的地址不会改变。但是使用 printf(%p, slice) 会改变。

英文:

I have this piece of code

package main

import (
    &quot;fmt&quot;
)

func Extend(slice []int, element int) []int {
    n := len(slice)
    if n == cap(slice) {
        // Slice is full; must grow.
        // We double its size and add 1, so if the size is zero we still grow.
        newSlice := make([]int, len(slice), 2*len(slice)+1)
        copy(newSlice, slice)
        slice = newSlice
    }
    slice = slice[0 : n+1]
    slice[n] = element
    return slice
}

func main() {
    slice := make([]int, 0, 5)
    for i := 0; i &lt; 10; i++ {
        slice = Extend(slice, i)
        fmt.Printf(&quot;len=%d cap=%d slice=%v\n&quot;, len(slice), cap(slice), slice)
        fmt.Println(&quot;address of 0th element:&quot;, &amp;slice[0])
        fmt.Println(&quot;address of slice:&quot;, &amp;slice) // why does this print the slice and not its address?
        fmt.Printf(&quot;address of slice: %p\n&quot;, &amp;slice) // why is this different from below? and why does it not change when a new slice is created pointing to a different array?
        fmt.Printf(&quot;address of slice: %p\n\n&quot;, slice)
    }
}

playground: https://play.golang.org/p/PWMN-i9_z9

My question on the second Println at the bottom in the loop. If you run it, you will see it prints out &[values...]. Why does it not print out the address? I know you can do it with Printf, among other ways, and it works, but what about Println? Println with &slice[0] works fine, it prints the address not the value, but Println with &slice is just like nope.

I also just noticed that when I do the Printf statement %p with &slice, vs then I do only slice, I get different addresses. Why? And the address with &slice does not change when it is changed (run it, the program resizes the array and creates a new slice). But printf(%p, slice) does change?

答案1

得分: 4

这是fmt.Println的定义。

来自https://golang.org/pkg/fmt/#Println:

Println使用其操作数的默认格式进行格式化,并写入标准输出。操作数之间始终添加空格,并附加换行符。它返回写入的字节数和遇到的任何写入错误。

在该页面的早些时候,描述了默认格式:

对于复合对象,元素将按照这些规则递归地打印出来,排列如下:

结构体:{字段0 字段1 ...}
数组、切片:[元素0 元素1 ...]
映射:map[键1:值1 键2:值2]
指向上述类型的指针:&{}, &[], &map[]

也许你的问题是“为什么”会是这样。答案有争议,但我认为这样显示数据比原始指针更有用。

你还问为什么修改切片不会改变其地址。切片是一个值类型(而不是引用类型),它包含指向底层数组的指针(以及其容量和当前长度)。将新值分配给切片类型的变量会覆盖该值,但不会更改其地址。这篇关于切片使用和内部原理的文章是一个很好的参考。

英文:

It's what fmt.Println is defined to do.

From https://golang.org/pkg/fmt/#Println :

> Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

And earlier on that page, describing the default formats:

> For compound objects, the elements are printed using these rules, recursively, laid out like this:
>
> struct: {field0 field1 ...}
> array, slice: [elem0 elem1 ...]
> maps: map[key1:value1 key2:value2]
> pointer to above: &{}, &[], &map[]

Maybe your question was "why" is it like this. The answer is debatable, but I suppose it's because it was considered more useful to show the data in this form rather than a raw pointer.

You also ask why modifying a slice doesn't change its address. A slice is a value type (not a reference type) which contains a pointer to the underlying array (as well as its capacity and current length). Assigning a new value to a variable of slice type overwrites the value, but doesn't change its address. This article on slice use and internals is a good reference.

huangapple
  • 本文由 发表于 2015年3月23日 12:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/29203551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定