• byte 实质上就是 uint8 类型。byte 用来强调数据是 raw data,而不是数字;
  • rune 实质上就是 int32 类型。而 rune 用来表示 Unicode 的 code point。
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
// 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

总结

  • 上边的用法叫做 类型别名: type alias = 已有类型,这个使用时不用进行显示类型转换

    ——也就是换了个名字,本质上还是一种类型

  • 还有一个常见的类似用法:type I int,这个叫做类型定义,使用时需要进行显示的类型转换

    ——声明了一种新类型

code

func main() {
	type Int int
	type IntEq = int

	var a int =3

	var I Int =3
	//I = a // cannot use a (type int) as type Int in assignment
	I = Int(a) //显示转换

	var Iq IntEq =3
	Iq = a // 隐式

	fmt.Println(I,Iq)
}

但是,如果直接赋值可以直接使用:

func main() {
    type Int int
    type IntEq = int

    var I Int =4
    var Iq IntEq =4
    fmt.Println(I,Iq)
}