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

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题