简体   繁体   中英

Address of slice in 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\n", 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\n", &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\n\n", 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM