golang []byte 怎么获取里面的数据
可以使用索引(下标)来获取[]byte中的数据,例如:
b := []byte{'h', 'e', 'l', 'l', 'o'}
fmt.Println(string(b[0])) // 输出: "h"
fmt.Println(string(b[1])) // 输出: "e"
fmt.Println(string(b[2])) // 输出: "l"
fmt.Println(string(b[3])) // 输出: "l"
fmt.Println(string(b[4])) // 输出: "o"
其中,b[0]表示获取b中的第一个元素,即字节'h';string(b[0])表示将字节转换为字符串,输出结果为"h"。
也可以使用for循环遍历整个[]byte:
b := []byte{'h', 'e', 'l', 'l', 'o'}
for _, v := range b {
fmt.Println(string(v))
}
输出结果为:
h
e
l
l
o
原文地址: https://www.cveoy.top/t/topic/scl 著作权归作者所有。请勿转载和采集!