Skip to content

Instantly share code, notes, and snippets.

@lboulard
Last active December 7, 2023 19:09
Show Gist options
  • Save lboulard/7392fe6434fd60380cea930a67e437c2 to your computer and use it in GitHub Desktop.
Save lboulard/7392fe6434fd60380cea930a67e437c2 to your computer and use it in GitHub Desktop.
slice.go #go #bugreport

Go playground: https://go.dev/play/p/9HKvgvSWqH3

start=0xc000100000   len=65536  cap=65536  end=0xc000110000   (start == nil) is false, (end == nil) is false, (buf == nil) is false
start=0xc000108000   len=   32  cap=32768  end=0xc000110000   (start == nil) is false, (end == nil) is false, (buf == nil) is false
start=0xc00010fff0   len=   16  cap=   16  end=0xc000110000   (start == nil) is false, (end == nil) is false, (buf == nil) is false
start=0xc00010ffff   len=    1  cap=    1  end=0xc000110000   (start == nil) is false, (end == nil) is false, (buf == nil) is false
start=0xc000100000   len=    0  cap=    0  end=0xc000100000   (start == nil) is false, (end == nil) is false, (buf == nil) is false
start=<nil>          len=    0  cap=    0  end=<nil>          (start == nil) is false, (end == nil) is false, (buf == nil) is true 

Program exited.

Expected start == nil and end == nil to be true.

package main
import (
"fmt"
"unsafe"
)
func t(buf []byte) {
start := unsafe.SliceData(buf)
end := unsafe.Add(unsafe.Pointer(start), cap(buf))
fmt.Printf(
"start=%-13v len=%5v cap=%5v end=%-13v (start == nil) is %-5v, (end == nil) is %-5v, (buf == nil) is %-5v\n",
start, len(buf), cap(buf), end, start == nil, end == nil, buf == nil,
)
}
func main() {
data := make([]byte, 64<<10)
t(data)
t(data[32768 : 32768+32])
t(data[cap(data)-16:])
t(data[cap(data)-1:])
t(data[cap(data):])
t(nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment