The maximum value for an int type in Go

前端 未结 10 1136
无人共我
无人共我 2021-01-29 19:09

How does one specify the maximum value representable for an unsigned integer type?

I would like to know how to initialize min in the loop below

10条回答
  •  你的背包
    2021-01-29 19:33

    I would use math package for getting the maximal value and minimal value :

    func printMinMaxValue() {
        // 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 flloat64= %+v\n", math.MaxFloat64)
        fmt.Printf("max float32= %+v\n", math.MaxFloat32)
    
        // etc you can see more int the `math`package
    }
    

    Ouput :

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

提交回复
热议问题