1. Home
  2. Docs
  3. golang
  4. 基础
  5. [] rune 和 [] byte 区别

[] rune 和 [] byte 区别

源码

// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8

// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32

byte 表示一个字节,rune 表示四个字节

    str := "home"

    println([]byte(str))
    println([]rune(str))
    //[4/32]0xc00003fea0
    //[4/32]0xc00003fec0

    println(str[1:2])

    str1 := "我的祖国他在东方"

    println([]byte(str1))
    println([]rune(str1))

    println(str1[1:2])

    stra := []rune(str1)
    println(string(stra[:2]))
    println(str1[:6])

    //[24/32]0xc00003fdf8
    //[8/32]0xc00003fe38

相关资料

go 的 [] rune 和 [] byte 区别

Was this article helpful to you? Yes No

How can we help?