Created
January 25, 2017 13:00
-
-
Save petergloor/1eacd3ebcfc3e09ed578dd0d4f80cabe to your computer and use it in GitHub Desktop.
MaxInt, MinInt, MaxUint and MinUint in Go (golang)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Release note Go 1.7: https://golang.org/doc/go1.17#math
The math package now defines three more constants:
MaxUint
,MaxInt
andMinInt
.
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
Release note Go 1.7: https://golang.org/doc/go1.17#math
The math package now defines three more constants:
MaxUint
,MaxInt
andMinInt
.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
nice