douwei1904 2015-03-23 04:59
浏览 148
已采纳

Golang中的切片地址

I have this piece of code

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
", len(slice), cap(slice), slice)
        fmt.Println("address of 0th element:", &slice[0])
        fmt.Println("address of slice:", &slice) // why does this print the slice and not its address?
        fmt.Printf("address of slice: %p
", &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("address of slice: %p

", 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条回答 默认 最新

  • douji6461 2015-03-23 05:13
    关注

    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.

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

报告相同问题?

悬赏问题

  • ¥20 fluent无法启动
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。