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

前端 未结 5 2150
我寻月下人不归
我寻月下人不归 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:22

    The fact that it's allowed a negative value lets you define your own rune sentinel values.

    For example:

    const EOF rune = -1
    
    func (l *lexer) next() (r rune) {
        if l.pos >= len(l.input) {
            l.width = 0
            return EOF
        }
        r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
        l.pos += l.width
        return r
    }
    

    Seen here in a talk by Rob Pike: Lexical Scanning in Go.

提交回复
热议问题