Length of an Array and the Language spec

The length is part of the array’s type; it must evaluate to a non-negative constant representable by a value of type int

If the length expression must evaluate to a non-negative constant, then wouldn’t the Type should be uint / unsigned int instead of int?

Does non-negative means Positive? if so, why use two word with compounding instead of one word (i.e) Positive. Thanks.

No. There is no right answer for handling overflow. In Go, they chose to make length overflow an illegal value, rather than ignoring it.

package main

import "fmt"

func main() {
	const maxInt = int(^uint(0) >> 1)
	var i int = maxInt
	fmt.Printf("%T: %d, %d\n", i, i, i+1)

	const maxUint = uint(^uint(0))
	var u uint = maxUint
	fmt.Printf("%T: %d, %d\n", u, u, u+1)
}
int: 9223372036854775807, -9223372036854775808
uint: 18446744073709551615, 0

No. Go uses the mathematics definition.

Terminology for signs

When 0 is said to be neither positive nor negative, the following phrases may refer to the sign of a number:

  • A number is positive if it is greater than zero.
  • A number is negative if it is less than zero.
  • A number is non-negative if it is greater than or equal to zero.
  • A number is non-positive if it is less than or equal to zero.

Wikipedia: Sign (mathematics): Terminology for signs

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.