Go string與[]byte slice的轉換方式如下。
範例環境:
- Go 1.17
下面把"字串"hello"轉為[]byte變數b內容為字母的ASCII碼,然後再轉回字串s。
main.go
package main
import "fmt"
func main() {
b := []byte("hello") // string to bytes slice
fmt.Println(b) // [104 101 108 108 111]
s := string(b) // bytes slice to string
fmt.Println(s) // hello
}
沒有留言:
張貼留言