首页 > 解决方案 > 如何将最小值/最大值设置为整数变量

问题描述

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

Integer.MIN_VALUE and Integer.MAX_VALUE

Go有什么办法吗?

标签: go

解决方案


它们在数学包中可用:

整数限制值。

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
)

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


推荐阅读