Skip to content

Instantly share code, notes, and snippets.

@perillo
Created December 31, 2014 15:21
Show Gist options
  • Save perillo/1ae8b20ef95fa550904a to your computer and use it in GitHub Desktop.
Save perillo/1ae8b20ef95fa550904a to your computer and use it in GitHub Desktop.
string-ref.go
// Check generate assembler with
// go tool 6g -S src/string-ref.go
package main
import (
"fmt"
"unsafe"
)
func do(b []byte) {
s := string(b)
b[0] = 'x'
fmt.Println(s)
}
func doref(b []byte) {
s := *(*string)(unsafe.Pointer(&b))
b[2] = 'y'
fmt.Println(s)
}
func main() {
b := []byte{'a', 'b', 'c'}
do(b)
doref(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment