我最初使用的代码取自@nmichaels在他的回答中使用的讨论线程。我现在使用一个稍微不同的计算方法。我提供了一些注释,以防其他人有与@Arijoon相同的查询

const (
    MinUint uint = 0                 // binary: all zeroes

    // Perform a bitwise NOT to change every bit from 0 to 1
    MaxUint      = ^MinUint          // binary: all ones

    // Shift the binary number to the right (i.e. divide by two)
    // to change the high bit to 0
    MaxInt       = int(MaxUint >> 1) // binary: all ones except high bit

    // Perform another bitwise NOT to change the high bit to 1 and
    // all other bits to 0
    MinInt       = ^MaxInt           // binary: all zeroes except high bit
)

最后两个步骤之所以有效,是因为正数和负数在二的补码运算中的表示方式。Numeric types上的Go语言规范部分向读者推荐了相关的Wikipedia article。我没有读过这本书,但我确实从Code by Charles Petzold一书中了解到了two的补充,这是一本非常容易理解的计算机和编码基础入门。

我将上面的代码(减去大部分注释)放入一个小integer math package中。