Skip to content

Instantly share code, notes, and snippets.

@petergloor
Created January 25, 2017 13:00
Show Gist options
  • Save petergloor/1eacd3ebcfc3e09ed578dd0d4f80cabe to your computer and use it in GitHub Desktop.
Save petergloor/1eacd3ebcfc3e09ed578dd0d4f80cabe to your computer and use it in GitHub Desktop.
MaxInt, MinInt, MaxUint and MinUint in Go (golang)
package main
import "fmt"
// Constant definitions
const MaxUint = ^uint(0)
const MinUint = 0
const MaxInt = int(^uint(0) >> 1)
const MinInt = -MaxInt - 1
func main() {
fmt.Println("Integer range on this computer")
fmt.Println("MinUint:", MinUint)
fmt.Println("MaxUint:", MaxUint)
fmt.Println("MinInt:", MinInt)
fmt.Println("MaxInt:", MaxInt)
}
@yingzhuo
Copy link

Nice!

@n0tduck1e
Copy link

nice

@olibre
Copy link

olibre commented Aug 19, 2021

Release note Go 1.7: https://golang.org/doc/go1.17#math

The math package now defines three more constants: MaxUint, MaxInt and MinInt.

https://play.golang.org/p/5R2iPasn6OZ

package main

import "fmt"
import "math"

const maxUint = uint(math.MaxUint)

func main() {
	fmt.Println("Integer range on your system")

	// .Println("MaxUint:", math.MaxUint)  ERROR constant 18446744073709551615 overflows int
	fmt.Println("MaxUint:", maxUint)

	fmt.Println("MinInt:", math.MinInt)
	fmt.Println("MaxInt:", math.MaxInt)
}

See also the SO answer: https://stackoverflow.com/a/68841425/93811

@n0tduck1e
Copy link

Release note Go 1.7: https://golang.org/doc/go1.17#math

The math package now defines three more constants: MaxUint, MaxInt and MinInt.

https://play.golang.org/p/mtBlkGG2kd0

package main

import (
	"fmt"
	"math"
)

var MaxUint uint = math.MaxUint

func main() {
	fmt.Println("Integer range on this computer")

	// .Println("MaxUint:", math.MaxUint)  ERROR constant 18446744073709551615 overflows int
	fmt.Println("MaxUint:", MaxUint)

	fmt.Println("MinInt:", math.MinInt)
	fmt.Println("MaxInt:", math.MaxInt)
}

finally, i kept forgetting the trick one. good bot !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment