Skip to content

Instantly share code, notes, and snippets.

@angristan
Created December 16, 2023 16:07
Show Gist options
  • Save angristan/9286543854a8c478f623b196bd3b949e to your computer and use it in GitHub Desktop.
Save angristan/9286543854a8c478f623b196bd3b949e to your computer and use it in GitHub Desktop.
Go slices
package main
import (
"fmt"
"reflect"
"unsafe"
)
func main() {
s := make([]string, 1, 2)
sh := (*reflect.SliceHeader)(unsafe.Pointer(&s))
fmt.Printf("%#v\n", sh)
fmt.Printf("%#v\n", s)
s[0] = "hello"
fmt.Printf("%#v\n", sh)
fmt.Printf("%#v\n", s)
s = append(s, "world")
fmt.Printf("%#v\n", sh)
fmt.Printf("%#v\n", s)
s = append(s, "!")
fmt.Printf("%#v\n", sh)
fmt.Printf("%#v\n", s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment