String Internals
Fill in the blank in line A, to convert b from []byte to string without causing memory copy.The expected output is "143"
package main
import (
"fmt"
"unsafe"
)
func main() {
var b = []byte("123")
s := __ //A
b[1] = '4'
fmt.Printf("%+v\n", s) //print 143
}
Answer
package main
import (
"fmt"
"unsafe"
)
func main() {
var b = []byte("123")
s := *(*string)(unsafe.Pointer(&b))
b[1] = '4'
fmt.Printf("%+v\n", s) //print 143
}