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.

Answer from peterSO on Stack Overflow
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)
}

🌐
Educative
educative.io › answers › what-is-type-float64-in-golang
What is type float64 in Golang?
The following code shows what happens ... the outputs of the code above, when we stored the max magnitude of a negative number of -1.7E+308 in num and printed it, the value came ......
Discussions

numbers - The maximum value for an int type in Go - Stack Overflow
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 that iteratively computes min and max lengths from s... More on stackoverflow.com
🌐 stackoverflow.com
July 30, 2011
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
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
go - Minimum positive float64 value - Stack Overflow
Just like This question, I need to a positive number that is as close to zero as possible, without it being zero, but in Go. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Reddit
reddit.com › r/golang › writing a function that returns the max value of a type [~float64 | ~uint | ~int t]
r/golang on Reddit: Writing a function that returns the max value of a type [~float64 | ~uint | ~int T]
March 17, 2024 -

Today I was trying to write a generic function to return the maximum value for floats, ints and uints. It seems straightforward, right? It's not!

The function signature:

type Types interface {
    ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint |
   	 ~int | ~int64 | ~int32 | ~int16 | ~int8 |
   	 ~float64 | ~float32
}

func infFor[T Types]() T

My first thought was to write a switch checking the type, like this:

func infFor[T Types]() T {
	var v T
	switch any(v).T {
	case float64:
    	return T(math.Inf(1))
	case int8:
    	return T(math.MaxInt8)
	...
	}
}

But this doesn't work for user defined types (type myInt int).

Then I tried to split the function in two parts, check if T is a float or (u)int, this makes the job easier.

func infFor[T Types]() T {
    // Check if T is a float.
    var f float64 = 1.5
    if float64(T(f)) == f {
   	 return T(math.Inf(1))
    }
    
	// Handle (u)ints ...
}

Converting 1.5 to an integer type will truncate the value and then float64(T(f)) == f is false.

The value 1.5 is important because it can be represented both by float64 and float32, 1.1 can't.

After the check we know the value is an integer, but the compiler doesn't, so we can't use bit arithmetic. The solution I found is to check when the value overflows.

This is the final version:

func infFor[T Types]() T {
    // Check if T is a float.
    var f float64 = 1.5
    if float64(T(f)) == f {
   	 return T(math.Inf(1))
    }

    maxValues := [...]uint64{
   	 math.MaxInt8,
   	 math.MaxUint8,
   	 math.MaxInt16,
   	 math.MaxUint16,
   	 math.MaxInt32,
   	 math.MaxUint32,
   	 math.MaxInt64,
   	 math.MaxUint64,
    }

    var v T
    // Check when v overflows.
    for i := 0; v+1 > 0; i++ {
   	 v = T(maxValues[i])
    }

    return v
}
🌐
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 type in Go is 1.79769313486231570814527423731704356798070e+308 and you can get this value using the math.MaxFloat64 constant.
🌐
YourBasic
yourbasic.org › golang › max-min-function
Compute max of two ints/floats · YourBasic Go
// Max returns the larger of x or y. func Max(x, y int64) int64 { if x < y { return y } return x } // Min returns the smaller of x or y. func Min(x, y int64) int64 { if x > y { return y } return x } For floats, use math.Max and math.Min.
🌐
Gophersnippets
gophersnippets.com › what-is-the-maximum-value-of-numeric-types
What is the maximum value of numeric types - GopherSnippets.com
package main import ( "fmt" "math" ... fmt.Println("Max float32:", math.MaxFloat32) fmt.Println("Max float64:", math.MaxFloat64) // Output: // Max int8: 127 // Max int16: 32767 // Max int32: 2147483647 // Max int64: 9223372036854775807 // Max uint8: 255 // Max uint16: 65535 ...
🌐
Go
go.dev › play › p › qeElniC-Nin
Go Playground - The Go Programming Language
Common problems companies solve with Go · Stories about how and why companies use Go
🌐
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...
Find elsewhere
🌐
W3Schools
w3schools.com › go › go_float_data_type.php
Go Float Data Types
package main import ("fmt") func main() { var x float64 = 1.7e+308 fmt.Printf("Type: %T, value: %v", x, x) } Try it Yourself »
🌐
Go Packages
pkg.go.dev › math › big
big package - math/big - Go Packages
A "p" or "P" exponent indicates a base 2 (rather than base 10) exponent; for instance, "0x1.fffffffffffffp1023" (using base 0) represents the maximum float64 value.
🌐
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] Whereas now I can achieve the same by simplifying using built-in min() method, without casting over and over the operands: titleMaxLength := min( MAX_TITLE_LENGTH, len((title)) )
🌐
GeeksforGeeks
geeksforgeeks.org › finding-maximum-of-two-numbers-in-golang
Finding Maximum of Two Numbers in Golang - GeeksforGeeks
April 28, 2025 - func Max(a, b float64) float64 · If you pass +Inf in this function like Max(+Inf, b) or Max(a, +Inf), then this function will return +Inf. If you pass NaN in this function like Max(NaN, b) or Max(a, NaN), then this function will return NaN. If you pass -0 in this function like Max(-0, -0), then this function will return -0.
🌐
Go Packages
pkg.go.dev › math
math package - math - Go Packages
Float64frombits returns the floating-point number corresponding to the IEEE 754 binary representation b, with the sign bit of b and the result in the same bit position. Float64frombits(Float64bits(x)) == x. ... Floor returns the greatest integer value less than or equal to x.
🌐
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
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).
🌐
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
🌐
Leapcell
leapcell.io › blog › understanding-floating-point-numbers-in-go
Understanding Floating-Point Numbers in Go | Leapcell
July 25, 2025 - On the other hand, float64, or ... the maximum value for float32 is approximately 3.4e38, while for float64, it's around 1.8e308....
🌐
IncludeHelp
includehelp.com › golang › math-maxfloat64-constant-with-examples.aspx
Golang math.MaxFloat64 Constant with Examples
August 25, 2021 - The return type of math.MaxFloat64 constant is float64, it returns the largest finite value of float64 type. // Golang program to demonstrate the // example of math.MaxFloat64 Constant package main import ( "fmt" "math" ) func main() { fmt.Printf("Type of math.MaxFloat64 is %T\n", math.MaxFloat64) ...