# How big are int types and uint types in golang?

Time：2019-12-18

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 (
"fmt"
_ "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)
}``````

Introduction

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

``````
package main
import (
"fmt"
"math"
"runtime"
)
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.

More

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