import (
    "fmt"
    "math"
)

func main() {
    // integer max
    fmt.Printf("max int64   = %+v\n", math.MaxInt64)
    fmt.Printf("max int32   = %+v\n", math.MaxInt32)
    fmt.Printf("max int16   = %+v\n", math.MaxInt16)

    // integer min
    fmt.Printf("min int64   = %+v\n", math.MinInt64)
    fmt.Printf("min int32   = %+v\n", math.MinInt32)

    fmt.Printf("max float64 = %+v\n", math.MaxFloat64)
    fmt.Printf("max float32 = %+v\n", math.MaxFloat32)

math

Output:

max int64   = 9223372036854775807 max int32   = 2147483647 max int16   = 32767 min int64   = ‑9223372036854775808 min int32   = ‑2147483648 max float64 = 1.7976931348623157e+308 max float32 = 3.4028234663852886e+38 

方法 4:

I originally used the code taken from the discussion thread that @nmichaels used in his answer. I now use a slightly different calculation. I've included some comments in case anyone else has the same query as @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
)

The last two steps work because of how positive and negative numbers are represented in two's complement arithmetic. The Go language specification section on Numeric types refers the reader to the relevant Wikipedia article. I haven't read that, but I did learn about two's complement from the book Code by Charles Petzold, which is a very accessible intro to the fundamentals of computers and coding.

I put the code above (minus most of the comments) in to a little integer math package.

方法 5:

mathmath.MaxUintmath.MaxIntmath.MinInt

Quick summary:

import "math/bits"
const (
    MaxUint uint = (1 << bits.UintSize) ‑ 1
    MaxInt int = (1 << bits.UintSize) / 2 ‑ 1
    MinInt int = (1 << bits.UintSize) / ‑2
)

Background:

uintuint32uint64

Note that it tends to be "faster" because using a non‑native type sometimes requires additional math and bounds‑checking to be performed by the processor, in order to emulate the larger or smaller integer. With that in mind, be aware that the performance of the processor (or compiler's optimised code) is almost always going to be better than adding your own bounds‑checking code, so if there is any risk of it coming into play, it may make sense to simply use the fixed‑size version, and let the optimised emulation handle any fallout from that.

With that having been said, there are still some situations where it is useful to know what you're working with.

uint1(1 << bits.UintSize) ‑ 1
uintuintint
const MaxUint uint = (1 << bits.UintSize) ‑ 1

That's the direct answer to your question, but there are also a couple of related calculations you may be interested in.

uintint
uintintuint
int21(1 << bits.UintSize) / 2 ‑ 1
int1‑2(1 << bits.UintSize) / ‑2

In summary:

(1 << bits.UintSize) ‑ 1
(1 << bits.UintSize) / 2 ‑ 1
(1 << bits.UintSize) / ‑2

full example (should be the same as below)

package main

import "fmt"
import "math"
import "math/bits"

func main() {
    var mi32 int64 = math.MinInt32
    var mi64 int64 = math.MinInt64
    
    var i32 uint64 = math.MaxInt32
    var ui32 uint64 = math.MaxUint32
    var i64 uint64 = math.MaxInt64
    var ui64 uint64 = math.MaxUint64
    var ui uint64 = (1 << bits.UintSize) ‑ 1
    var i uint64 = (1 << bits.UintSize) / 2 ‑ 1
    var mi int64 = (1 << bits.UintSize) / ‑2
    
    fmt.Printf(" MinInt32: %d\n", mi32)
    fmt.Printf(" MaxInt32:  %d\n", i32)
    fmt.Printf("MaxUint32:  %d\n", ui32)
    fmt.Printf(" MinInt64: %d\n", mi64)
    fmt.Printf(" MaxInt64:  %d\n", i64)
    fmt.Printf("MaxUint64:  %d\n", ui64)
    fmt.Printf("  MaxUint:  %d\n", ui)
    fmt.Printf("   MinInt: %d\n", mi)
    fmt.Printf("   MaxInt:  %d\n", i)
}

(by Mike Samuel、nmichaels、Deleted、Gujarat Santana、crantok、Will Palmer)

參考文件