Home > Software design >  What's the easiest way to write INT_MAX in Golang?
What's the easiest way to write INT_MAX in Golang?

Time:03-31

I have always used INT_MAX in C and I'm trying to find an easy way to do it in Golang.

I did try INT_MAX and uint32 but they didn't work, and math.MaxInt is lengthy as I'll have to import math library.

CodePudding user response:

All such numeric constants are defined in the math package. Use math.MaxInt

https://pkg.go.dev/[email protected]#pkg-constants

Importing a package is not the same as importing a library. You are only importing the names in that package. No function in math library will be included in the final binary unless you use them.

CodePudding user response:

As you don't want to import math package or any other library

You can use the following value

  1. MaxUint64 = (1 << 64) - 1
  2. MaxUint32 = (1 << 32) - 1
  3. MaxInt64 = (1 << 63) - 1
  4. MaxInt32 = (1 << 31) - 1

Official golang's documentation reference link

CodePudding user response:

I found an easy way. We can use the bitwise operator 1 << 31.

  • Related