Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set minimum/maximum value to an Integer variable [duplicate]

Tags:

go

Like in Java we initialise variable to minimum/maximum,

Integer.MIN_VALUE and Integer.MAX_VALUE

Is there any way to do in Go?

like image 845
infiniteLearner Avatar asked Oct 20 '25 16:10

infiniteLearner


2 Answers

They are available in math package:

Integer limit values.

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

like image 194
zch Avatar answered Oct 23 '25 08:10

zch


I found solution .

package main

import (
    "fmt"
    "math"
)

func main() {
    // Few Examples

    // 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)

    // Float
    fmt.Printf("Max flloat64= %v\n", math.MaxFloat64)
    fmt.Printf("Max float32= %v\n", math.MaxFloat32)

}
like image 27
infiniteLearner Avatar answered Oct 23 '25 07:10

infiniteLearner