(转)Go语言中的byte和rune
2018-07-10 本文已影响317人
one_zheng
Go语言中byte和rune实质上就是uint8和int32类型。
golang byte和rune.png参考规范:
uint8 the set of all unsigned 8-bit integers (0 to 255)
int32 the set of all signed 32-bit integers (-2147483648 to 2147483647)
byte alias for uint8
rune alias for int32
可以通过下面程序验证:
package main
import "fmt"
func byteSlice(b []byte) []byte {
return b
}
func runeSlice(r []rune) []rune {
return r
}
func main() {
b := []byte{0, 1}
u8 := []uint8{2, 3}
fmt.Printf("%T %T \n", b, u8)
fmt.Println(byteSlice(b))
fmt.Println(byteSlice(u8))
r := []rune{4, 5}
i32 := []int32{6, 7}
fmt.Printf("%T %T \n", r, i32)
fmt.Println(runeSlice(r))
fmt.Println(runeSlice(i32))
}
执行结果如下:
[]uint8 []uint8
[0 1]
[2 3]
[]int32 []int32
[4 5]
[6 7]