Skip to content

Instantly share code, notes, and snippets.

@foolishway
Created April 12, 2020 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foolishway/1630080596f7b3888038c687fed77a92 to your computer and use it in GitHub Desktop.
Save foolishway/1630080596f7b3888038c687fed77a92 to your computer and use it in GitHub Desktop.
stringHeader.go
package main
import (
"fmt"
"unsafe"
)
func main() {
b := []byte("some momery")
s := string(b)
b[0] = 'z'
fmt.Printf("string(b)=%s, s=%s\n", string(b), s)
type stringHeader struct {
p *byte
len int
}
s = *(*string)(unsafe.Pointer(&stringHeader{p: &b[0], len: len(b)}))
fmt.Printf("s=%s\n", s)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment