The math package has a constant for this and similar values, just use that: math.SmallestNonzeroFloat64:

const (
    MaxFloat32             = 3.40282346638528859811704183484516925440e+38  // 2**127 * (2**24 - 1) / 2**23
    SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)

    MaxFloat64             = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
    SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)

Printing its value:

fmt.Println(math.SmallestNonzeroFloat64)

Outputs (try it on the Go Playground):

5e-324

(Note: it's greater than the constant due to rounding in the fmt package.)

Answer from icza on Stack Overflow
🌐
gosamples
gosamples.dev › tutorials › the maximum and minimum value of the float types in go
📊 The maximum and minimum value of the float types in Go
April 14, 2022 - The maximum value of the float64 ... The minimum value above zero (smallest positive, non-zero value) of the float64 type in Go is 4.9406564584124654417656879286822137236505980e-324 and you can get this value using the math.SmallestNonzeroFloat64 constant....
Discussions

Standard lib math package - why only float64?
Integer arithmetic and floating point arithmetic are very different. float64 makes the intent clear and ensures there is no ambiguity if the code is executed on a 32-bit system. More on reddit.com
🌐 r/golang
23
27
January 17, 2024
Proposal: Add Min and Max functions for non float64 types.
The current math library only deals with float64, so when you have to deal with integers you have 2 options: Reinvent the wheel and write your own min and max functions for the type you need. Cast ... More on github.com
🌐 github.com
4
May 27, 2018
variables - The maximum value for float64 and complex128 type in Go - Stack Overflow
I need to know the maximum value of float64 and complex128 variable types in golang. go doesn't seem to have an equivalent of float.h and I don't know how to calculate it. More on stackoverflow.com
🌐 stackoverflow.com
What is the correct way to find the min between two integers in Go? - Stack Overflow
golang makes you rewrite the wheel each time you need min for an other type that float64 ? More on stackoverflow.com
🌐 stackoverflow.com
🌐
DEV Community
dev.to › sw360cab › built-in-min-and-max-methods-in-go-121-24ph
Built-in min() and max() methods in Go 1.21 - DEV Community
April 24, 2024 - const MAX_TITLE_LENGTH int = 50 // a placeholder title var title string = "..." // the given title titleMaxLength := int( math.Min( float64(MAX_TITLE_LENGTH), float64(len(title)) ) ) croppedTitle := title[:titleMaxLength]
🌐
GeeksforGeeks
geeksforgeeks.org › go language › finding-minimum-of-two-numbers-in-golang
Finding Minimum of Two Numbers in Golang - GeeksforGeeks
April 13, 2020 - func Min(a, b float64) float64 · If you pass -Inf in this function like Min(-Inf, b) or Min(a, -Inf), then this function will return -Inf. If you pass NaN in this function like Min(NaN, b) or Min(a, NaN), then this function will return NaN.
🌐
GitHub
github.com › golang › go › issues › 25597
Proposal: Add Min and Max functions for non float64 types. · Issue #25597 · golang/go
May 27, 2018 - Cast everything to float64 and back. Neither of these options are ideal, so I would like to propose adding the following 2 functions: math.MinInt(x, y int) int math.MaxInt(x ,y int) int
Published   May 27, 2018
Author   svenwiltink
Find elsewhere
Top answer
1 of 2
40

For example,

package main

import (
    "fmt"
    "math"
)

func main() {
    const f = math.MaxFloat64
    fmt.Printf("%[1]T %[1]v\n", f)
    const c = complex(math.MaxFloat64, math.MaxFloat64)
    fmt.Printf("%[1]T %[1]v\n", c)
}

Output:

float64 1.7976931348623157e+308
complex128 (1.7976931348623157e+308+1.7976931348623157e+308i)

Package math

import "math" 

Floating-point limit values. Max is the largest finite value representable by the type. SmallestNonzero is the smallest positive, non-zero value representable by the type.

const (
        MaxFloat32             = 3.40282346638528859811704183484516925440e+38  // 2**127 * (2**24 - 1) / 2**23
        SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)

        MaxFloat64             = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
        SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)

The Go Programming Language Specification

Numeric types

A numeric type represents sets of integer or floating-point values. The predeclared architecture-independent numeric types are:

uint8       the set of all unsigned  8-bit integers (0 to 255)
uint16      the set of all unsigned 16-bit integers (0 to 65535)
uint32      the set of all unsigned 32-bit integers (0 to 4294967295)
uint64      the set of all unsigned 64-bit integers (0 to 18446744073709551615)

int8        the set of all signed  8-bit integers (-128 to 127)
int16       the set of all signed 16-bit integers (-32768 to 32767)
int32       the set of all signed 32-bit integers (-2147483648 to 2147483647)
int64       the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

complex64   the set of all complex numbers with float32 real and imaginary parts
complex128  the set of all complex numbers with float64 real and imaginary parts

byte        alias for uint8
rune        alias for int32

The value of an n-bit integer is n bits wide and represented using two's complement arithmetic.

There is also a set of predeclared numeric types with implementation-specific sizes:

uint     either 32 or 64 bits
int      same size as uint
uintptr  an unsigned integer large enough to store the uninterpreted bits of a pointer value

To avoid portability issues all numeric types are distinct except byte, which is an alias for uint8, and rune, which is an alias for int32. Conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture.

2 of 2
5

You can also consider using the Inf method from the math package which returns a value for infinity (positive or negative if you want), but is considered to be float64.

Not too sure if there is an argument for one or the other between math.MaxFloat64 and math.Inf(). Comparing the two I've found that Go interprets the infinity values to be larger than the max float ones.

package main

import (
    "fmt"
    "math"
)

func main() {
    infPos := math.Inf(1) // gives positive infinity
    fmt.Printf("%[1]T %[1]v\n", infPos)
    
    infNeg := math.Inf(-1) // gives negative infinity
    fmt.Printf("%[1]T %[1]v\n", infNeg)
}

🌐
IncludeHelp
includehelp.com › golang › math-min-function-with-examples.aspx
Golang math.Min() Function with Examples
The return type of Min() function is a float64, it returns the smaller value of the given parameters. Min(x, -Inf) = Min(-Inf, x) = -Inf If any of the any parameter is negative infinity, the function returns -Inf. Min(x, NaN) = Min(NaN, x) = NaN If any of the parameter is NaN, the function ...
Top answer
1 of 9
184

Starting with Go 1.21, min and max are available as builtins and you do not need to write them at all. (Thanks @ufukty for highlighting this in a comment.)

I'm leaving the previous content below in case someone needs a different simple generic function that isn't built in or can otherwise use the historical answers.


Until Go 1.18 a one-off function was the standard way; for example, the stdlib's sort.go does it near the top of the file:

func min(a, b int) int {
    if a < b {
        return a
    }
    return b
}

You might still want or need to use this approach so your code works on Go versions below 1.18!

Starting with Go 1.18, you can write a generic min function which is just as efficient at run time as the hand-coded single-type version, but works with any type with < and > operators:

func minT constraints.Ordered T {
    if a < b {
        return a
    }
    return b
}

func main() {
    fmt.Println(min(1, 2))
    fmt.Println(min(1.5, 2.5))
    fmt.Println(min("Hello", "世界"))
}

There's been discussion of updating the stdlib to add generic versions of existing functions, but if that happens it won't be until a later version.

math.Min(2, 3) happened to work because numeric constants in Go are untyped. Beware of treating float64s as a universal number type in general, though, since integers above 2^53 will get rounded if converted to float64.

2 of 9
23

There is no built-in min or max function for integers, but it’s simple to write your own. Thanks to support for variadic functions we can even compare more integers with just one call:

func MinOf(vars ...int) int {
    min := vars[0]

    for _, i := range vars {
        if min > i {
            min = i
        }
    }

    return min
}

Usage:

MinOf(3, 9, 6, 2)

Similarly here is the max function:

func MaxOf(vars ...int) int {
    max := vars[0]

    for _, i := range vars {
        if max < i {
            max = i
        }
    }

    return max
}
🌐
unexpected-go
unexpected-go.com › theres-no-min-function.html
There is no math.Min(int, int) int function | unexpected-go
package main import ( "fmt" "math" ) func main() { a := 1 b := 2 m := math.Min(float64(a), float64(b)) fmt.Println(m) } (Note: casting ints into floats may result in loss of precision) Defining your own min function everywhere is an option: package main import ( "fmt" ) func main() { a := 1 b := 2 m := min(a, b) fmt.Println(m) } func min(a, b int) int { if a < b { return a } return b } Go 1.18 introduced support for generics, and now a single function can be defined for all the comparable types: package main import ( "fmt" "golang.org/x/exp/constraints" ) func min[T constraints.Ordered](a, b T) T { if a < b { return a } return b } func main() { fmt.Println(min(1, 2)) fmt.Println(min(1.5, 2.7)) }
🌐
Blogger
mrekucci.blogspot.com › 2015 › 07 › dont-abuse-mathmax-mathmin.html
mrekucci's blog: Don't abuse math.Max / math.Min
Many people, especially newcomers to Go (of course, not all of them), tend to abuse the math.Min and math.Max functions from the math p...
🌐
Jeromewu
jeromewu.github.io › generics-in-golang-1.18-no-need-to-write-max-min-anymore-maybe
Generics in Golang 1.18, no need to write max / min anymore (maybe) | TechBlog
Write a wrapper function for math.Max or math.Min to do type conversion · func max(x, y int) int { return int(math.Max(float64(x), float64(y))) } ... You might think it is not too hard to implement any of the solutions above, but it is just a totally waste of time, especially when you are joining online contests or just practicing algorithms and data structure. So with the new accepted proposal in golang 1.18, finally we can fix this issue once and for all (if the built-in math.Max & math.Min is updated).
🌐
Educative
educative.io › answers › what-is-type-float64-in-golang
What is type float64 in Golang?
A variable of type float64 can store decimal numbers ranging from 2.2E-308 to 1.7E+308.
🌐
PixelsTech
pixelstech.net › home › articles › why no max/min function for integer in golang
Why no max/min function for integer in GoLang | PixelsTech
August 6, 2019 - GoLang intentionally omits built-in max/min functions for integer types, a common feature in other languages. While math.Max and math.Min exist for float64, this is due to the inherent complexity of floating-point comparisons, which Go provides ...
🌐
Medium
medium.com › techtrends-digest › golang-why-float64-betrays-you-and-when-to-use-decimal-instead-41194e13601a
Golang: Why float64 Betrays You and When to Use Decimal Instead
December 2, 2025 - Golang: Why float64 Betrays You and When to Use Decimal Instead A practical guide to choosing the right number type for money, precision, and real-world calculations. Developers new to Go often …
🌐
Hugo
gohugo.io › functions › math › min
math.Min
April 10, 2025 - math.Min VALUE... Returns · float64 · {{ math.Min 1 (slice 2 3) 4 }} → 1 · Last updated: April 10, 2025 : Merge commit '5be51ac3db225d5df501ed1fa1499c41d97dbf65' (283e97783)Improve this page · math.Abs · math.Acos · math.Add · math.Asin · math.Atan · math.Atan2 ·