Created
April 12, 2020 14:11
-
-
Save foolishway/1630080596f7b3888038c687fed77a92 to your computer and use it in GitHub Desktop.
stringHeader.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
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