Golang String to Byte Conversion: A Simple Guide
To convert a Go string to bytes, you can use the []byte type conversion. Here's an example:
package main
import "fmt"
func main() {
str := 'Hello, World!'
bytes := []byte(str)
fmt.Println(bytes) // [72 101 108 108 111 44 32 87 111 114 108 100 33]
}
In the above example, the []byte conversion is used to convert the string str to a byte slice called bytes. The fmt.Println statement is used to print the byte slice. Each character in the string is represented by its ASCII value in the byte slice.
原文地址: https://www.cveoy.top/t/topic/qbJb 著作权归作者所有。请勿转载和采集!