package main
import (
"fmt"
"unsafe"
)
func main() {
str := "hello world"
//str[0] = 'H' //cannot assign to str[0] (value of type byte)
ptr := unsafe.Pointer(&str)
fmt.Println(unsafe.Sizeof(str), ptr)
//(*(*[]byte)(ptr))[0] = 'H' // unexpected fault address 0xae5728
*(*[]byte)(ptr) = []byte("123456")
*(*int)(unsafe.Add(ptr, 8)) = 100
fmt.Println("字符串长度:", len(str))
fmt.Println("字符串内容:", "->", str, "<-")
fmt.Println(&str)
fmt.Println(ptr)
}
go string 是只读的
于 2022-07-27 14:12:44 首次发布