Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why not uint32 instead of int64? [closed]

Tags:

go

There are some similar designs in the go interface. For example, the results of reading and writing can only be values >= 0. Why not just use the unsigned int type? What is the purpose of using signed types?

// The Copy function uses ReaderFrom if available.
type ReaderFrom interface {
    ReadFrom(r Reader) (n int64, err error)
}

// The Copy function uses WriterTo if available.
type WriterTo interface {
    WriteTo(w Writer) (n int64, err error)
}
like image 865
Time Killer Avatar asked Sep 05 '25 05:09

Time Killer


1 Answers

I found a good explanation in the book Go Programming Language (A. Donovan, B. Kernighan):

Although Go provides unsigned numbers and arithmetic, we tend to use the signed int form even for quantities that can’t be negative, such as the length of an array, though uint might seem a more obvious choice. Indeed, the built-in len function returns a signed int, as in this loop which announces prize medals in reverse order:

medals := []string{"gold", "silver", "bronze"}
    for i := len(medals) - 1; i >= 0; i-- {
    fmt.Println(medals[i]) // "bronze", "silver", "gold"
}

The alternative would be calamitous. If len returned an unsigned number, then i too would be a uint, and the condition i >= 0 would always be true by definition. After the third iteration, in which i == 0, the i-- statement would cause i to become not −1, but the maximum uint value, and the evaluation of medals[i] would fail at run time, or panic, by attempting to access an element outside the bounds of the slice. For this reason, unsigned numbers tend to be used only when their bitwise operators or peculiar arithmetic operators are required, as when implementing bit sets, parsing binary file.

like image 181
Anton Avatar answered Sep 07 '25 20:09

Anton