Created
December 31, 2014 15:21
-
-
Save perillo/1ae8b20ef95fa550904a to your computer and use it in GitHub Desktop.
string-ref.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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