可以使用标准库中的string()函数将字节数组转换成字符串。

示例代码:

package main

import "fmt"

func main() {
    bytes := []byte{'h', 'e', 'l', 'l', 'o'}
    str := string(bytes)
    fmt.Println(str) // 输出:hello
}

另外,如果字节数组中存储的是 UTF-8 编码的字符,也可以使用string()函数将其转换成字符串。

示例代码:

package main

import "fmt"

func main() {
    bytes := []byte{0xe4, 0xbd, 0xa0, 0xe5, 0xa5, 0xbd}
    str := string(bytes)
    fmt.Println(str) // 输出:你好
}
golang 中如何将字节数组转换成字符串

原文地址: http://www.cveoy.top/t/topic/GKB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录