1. 程式人生 > >golang中的byte、rune對應的基礎型別

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
}