大地缸
大地缸
Post - Soft - Tags - Links - About
编程爱好者
Golang中[]byte和string互转
Dec 7, 2020
1 min read
from
package main
import (
"fmt"
_ "unsafe"
)
func main() {
bytes := []byte("I am byte array !")
str := string(bytes)
bytes[0] = 'i'//注意这一行,bytes在这里修改了数据,但是str打印出来的依然没变化,
fmt.Println(str)
}
to
package main
import (
"fmt"
"unsafe"
)
func main() {
bytes := []byte("I am byte array !")
str := (*string)(unsafe.Pointer(&bytes))
bytes[0] = 'i'
fmt.Println(*str)
}
有疑问加站长微信联系(非本文作者)