Skip to content

Golang: Number Types Maximum and Minimum Value (Limit Values)

Dear math package has a very good set of constants that help you do your job with confidence. You can use Max or Min constants:

math.MaxInt64
math.MinInt64

This working example prints the maximum and minimum numbers of different types like int, int16, int32, int64, float32, and float64:

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println("Max Int16", math.MaxInt16)
	fmt.Println("Max Int32", math.MaxInt32)
	fmt.Println("Max Int64", math.MaxInt64)
	fmt.Println("Max Int  ", math.MaxInt)

	fmt.Println("Max Float32", math.MaxFloat32)
	fmt.Println("Max Float64", math.MaxFloat64)

	fmt.Println("Min Int16", math.MinInt16)
	fmt.Println("Min Int32", math.MinInt32)
	fmt.Println("Min Int64", math.MinInt64)
	fmt.Println("Min Int  ", math.MinInt)

	fmt.Println("Max Float32", -1 * math.MaxFloat32)
	fmt.Println("Max Float64", -1 * math.MaxFloat64)
}

Leave a Reply

Your email address will not be published. Required fields are marked *