golang中的byte、rune对应的基础类型

golang中存在丰富的类型,其中一部分类型如byte、rune等是基于底层的整形等基础类型实现的,熟悉类型对应的基础类型能够帮助我们了解一些coding中的小技巧,可以基于类型的基础类型直接使用。
首先了解一下一些内建类型对应的基础类型,这些都可以在源码中builtin.go中找到:

// 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

// iota is a predeclared identifier representing the untyped integer ordinal
// number of the current const specification in a (usually parenthesized)
// const declaration. It is zero-indexed.
const iota = 0 // Untyped int.

如下一些小技巧:

s:="abc"
cs := make([]int, 128)
for _, c := range s {
   cs[c] += 1
}

你可能感兴趣的:(go)