Why is rune in golang an alias for int32 and not uint32?

前端 未结 5 2092
我寻月下人不归
我寻月下人不归 2021-01-31 15:10

The type rune in Go is defined as

an alias for int32 and is equivalent to int32 in all ways. It is used, by conv

5条回答
  •  遥遥无期
    2021-01-31 15:40

    In addition to the above answers given, here are my two cents to why Go needed rune.

    • Strings in GoLang are byte arrays with each character being represented as a single byte. Thus GoLang has a very high-performance advantage when compared to other languages
    • But since we need a way to represent UTF-8 codepoints which cannot be represented with an 8bit range, we use the rune to represent them.
    • Why int32 and why not uint32? you may ask. This is made deliberately to detect overflows while doing operations on strings.

    this article talks all these in much more details

提交回复
热议问题