i originally posted this on reddit but i havent really been able to get the answers im looking for so im trying to branch out. I hope its not an issue linking to other questions on other sites.
I wrote a quick bit of code to go through the issue i have with slices and made separate cases for each problem. I’ll address them individually as they create some real inconsistencies in slice behaviour.
package main
import (
"fmt"
)
func printfSlice(slc []int, itr int) {
fmt.Printf("\ncount : %v", itr)
fmt.Printf("\nslice01 val : %v"+
"\nslice01 addr : %p "+
"\nslice length : %v "+
"\nslice01 cap : %v ",
slc, &slc, len(slc), cap(slc))
fmt.Printf("\n")
}
func main() {
fmt.Println("start")
slice01 := []int{2, 3, 5, 7, 11, 23}
count01 := 0
//count 1
count01++
printfSlice(slice01, count01)
//count 2
count01++
slice01 = slice01[0:0]
printfSlice(slice01, count01)
//count 3
count01++
slice01 = slice01[1:5]
printfSlice(slice01, count01)
//count 4
count01++
slice01 = slice01[:3]
printfSlice(slice01, count01)
//count 5
count01++
slice01 = slice01[:]
printfSlice(slice01, count01)
//count 6
count01++
slice01 = slice01[0:cap(slice01)]
printfSlice(slice01, count01)
//count 7
count01++
slice01 = slice01[1:4]
printfSlice(slice01, count01)
and the output
count : 1
slice01 val : [2 3 5 7 11 23]
slice01 addr : 0xc000046420
slice length : 6
slice01 cap : 6
count : 2
slice01 val : []
slice01 addr : 0xc000046460
slice length : 0
slice01 cap : 6
count : 3
slice01 val : [3 5 7 11]
slice01 addr : 0xc0000464a0
slice length : 4
slice01 cap : 5
count : 4
slice01 val : [3 5 7]
slice01 addr : 0xc0000464e0
slice length : 3
slice01 cap : 5
count : 5
slice01 val : [3 5 7]
slice01 addr : 0xc000046520
slice length : 3
slice01 cap : 5
count : 6
slice01 val : [3 5 7 11 23]
slice01 addr : 0xc000046560
slice length : 5
slice01 cap : 5
count : 7
slice01 val : [5 7 11]
slice01 addr : 0xc0000465a0
slice length : 3
slice01 cap : 4
delve closed with code 0
a) pointers and addresses:
i understand that slices are pointers, but all the values end up with different addresses. if theyre all pointing back to the same array, shouldnt they all have the same address?
b) what is the actual underlying object everything is pointing to? :
in sect 2 of the output, i select the slice to length 0 by selecting nothing. then in sect 3, I select slice01[1:5] which gets me [3 5 7 11] , which makes no sense. I’ve defined slice01 as nothing in the previous section, so why wouldnt it return nothing or an error? It also now has reduced the capacity to 5. Why? it should just reduce the length not the capacity? why did making a specific selection in a slice reduce the capacity but selecting nothing didnt?
Then in sect 4 i select slice01 = slice01[:3] (remember im reusing using the SAME variable every single time). But instead of [2 3 5] from the original slice i get [3 5 7] from the slice defined in sect 3. This is even more confusing. In sect 3 it selected from sect 1 even though i redefined the variable in sect 2! and now in section 4 when i make another slice selection, it selects from sect 3 not sect 1? wtf is this?
sect 5, 6 and 7 continue to demonstrate the same problem.
what am i missing here? why do slices behave like this? is there some kind of pattern or rule that im missing?