How big are int types and uint types in golang?


Before we start, let’s look at the difference between uint and int

Above is the figure, and below is the source code:

package main 
import ( 
_ "time" 
func main() { 
A: = byte (255) // 11111111 this is the limit of byte, because a: = byte (256) // out of range error report, 0-255 is exactly 256, can't be higher 
B: = uint8 (255) // 11111111 this is the limit of uint8, because C: = uint8 (256) // out of bounds error reporting, 0-255 is exactly 256, can't be higher 
C: = int8 (127) // 011111111 this is the limit of int8, because B: = int8 (128) // error is reported out of bounds, 0-127 is exactly 128 numbers, so the limit of int8 is only half of 256 
D: = int8 (a) // 11111111 will print out - 0000001, int8 (128), int8 (255), int8 (byte (255)) will report error and out of range, because the int limit is 127, but it can be written as: int8 (a), the first bit is used as a symbol 
E: = int8 (c) // 011111111 printed or 011111111 
fmt.Printf("%08b %d \n", a, a) 
fmt.Printf("%08b %d \n", b, b) 
fmt.Printf("%08b %d \n", c, c) 
fmt.Printf("%08b %d \n", d, d) 
fmt.Printf("%08b %d \n", e, e) 


Today, I debugged a problem and found a situation I couldn’t understand:

package main
import (
func main() {
 var a uint = math.MaxUint64
 fmt.Println("Hello, playground", a, runtime.Version())

Assign a 64 bit number to uint. I understand that uint is 32-bit. Why can it be compiled? But I tried it on the playground again, and the result is that it can’t be compiled:

constant 18446744073709551615 overflows uint

How much space does int and uint occupy?

In fact, I always understand that it’s 32-bit. Because that’s what other languages are like, thinking in inertia.

Take a look at the official documents:

int is a signed integer type that is at least 32 bits in size. It is a distinct type, however, and not an alias for, say, int32.

Uint and int are similar. That is to say, this shaping occupies at least 32 bits. Int and int32 are two different things.

Let’s take a look at Dave Cheney’s reply (he replied to GitHub in the middle of the night so dedicated):

uint is a variable sized type, on your 64 bit computer uint is 64 bits wide.

My understanding of uint type length depends on CPU. If it is 32-bit CPU, it is 4 bytes, if it is 64 bit, it is 8 bytes. My computer is 64 bit, and the playground is 32-bit. The problem is here.


There will be a situation here. Int and uint are based on CPU changes. How to know the current system situation?

  • CPU model:
  • Length of int: strconv.intsize

I’ve written golang and int for so many years everyday, and I’ve been treating them as 32-bit. I’m ashamed…


The above is the whole content of this article. I hope that the content of this article has a certain reference learning value for everyone’s study or work. If you have any questions, you can leave a message and exchange. Thank you for your support for developepaar.

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]