Skip to content

Instantly share code, notes, and snippets.

@reflechant
Created September 16, 2024 08:33
Show Gist options
  • Save reflechant/906587f821e67e3cc958f43fce5d814f to your computer and use it in GitHub Desktop.
Save reflechant/906587f821e67e3cc958f43fce5d814f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"unsafe"
)
func stringptr(s string) *byte {
return unsafe.StringData(s)
}
func sliceptr(s []uint64) *uint64 {
return unsafe.SliceData(s)
}
func A() string {
return "an example string"
}
func B() string {
return "an example string"
}
func C() []uint64 {
return []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9}
}
func D() []uint64 {
return []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9}
}
func main() {
fmt.Println("STRING INTERNING IN GO")
fmt.Printf("first string underlying array address: %v\n", stringptr(A()))
fmt.Printf("second string underlying array address: %v\n", stringptr(B()))
fmt.Printf("Are the string addresses equal? %v\n", stringptr(A()) == stringptr(B()))
fmt.Println("NO SLICE INTERNING IN GO")
fmt.Printf("first slice underlying array address: %v\n", sliceptr(C()))
fmt.Printf("second slice underlying array address: %v\n", sliceptr(D()))
fmt.Printf("Are the slice addresses equal? %v\n", sliceptr(C()) == sliceptr(D()))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment