Maximum value for int type in Go: how to get and use it
Hi everyone! I just started learning Go and encountered a confusing situation. I need to find the maximum value for the `int` type in Go to check if my variable will go out of range. In other languages, like C or Java, I simply used constants like `INT_MAX` or `Integer.MAX_VALUE`. But in Go, I didn't find anything similar in the standard library.
I tried writing this code:
```go
package main
import "fmt"
func main() {
var a int = 2147483647
fmt.Println(a)
a = a + 1
fmt.Println(a) // expected an error, but got a negative number
}
```
When I increment a large number, it becomes negative. This is overflow, right? How can I find out what the exact maximum value for `int` is on my platform? After all, `int` in Go can be
No answers yet.
Log in to leave an answer
Blogs