如何将最小值/最大值设置为整数变量

inf*_*ner -1 go

就像在 Java 中一样,我们将变量初始化为最小值/最大值,

Integer.MIN_VALUE and Integer.MAX_VALUE
Run Code Online (Sandbox Code Playgroud)

Go有什么办法吗?

zch*_*zch 5

它们在数学包中可用:

整数限制值。

const (
    MaxInt8   = 1<<7 - 1
    MinInt8   = -1 << 7
    MaxInt16  = 1<<15 - 1
    MinInt16  = -1 << 15
    MaxInt32  = 1<<31 - 1
    MinInt32  = -1 << 31
    MaxInt64  = 1<<63 - 1
    MinInt64  = -1 << 63
    MaxUint8  = 1<<8 - 1
    MaxUint16 = 1<<16 - 1
    MaxUint32 = 1<<32 - 1
    MaxUint64 = 1<<64 - 1
)
Run Code Online (Sandbox Code Playgroud)

https://golang.org/pkg/math/#pkg-constants