Entiers (Integers)
Les entiers peuvent être globalement divisés en deux catégories : les entiers non signés (unsigned integers) et les entiers signés (signed integers). Les entiers signés sont les plus largement utilisés.
Non signé (Unsigned) signifie qu'il ne peut représenter que des nombres non négatifs (0 et les nombres positifs), tandis que les nombres signés (signed) peuvent représenter à la fois les nombres négatifs et non négatifs.
Les entiers non signés peuvent être divisés en quatre tailles : 8 bits, 16 bits, 32 bits et 64 bits, représentés respectivement par uint8, uint16, uint32 et uint64. Les entiers signés correspondants sont int8, int16, int32 et int64. Le tableau suivant indique les différentes plages représentées par chaque type :
| Type |
Description |
Plage (Range) |
| uint8 |
Entier non signé 8 bits (8-bit unsigned int) |
0 à 255 |
| int8 |
Entier signé 8 bits (8-bit signed int) |
-128 à 127 |
| uint16 |
Entier non signé 16 bits (16-bit unsigned int) |
0 à 65535 |
| int16 |
Entier signé 16 bits (16-bit signed int) |
-32768 à 32767 |
| uint32 |
Entier non signé 32 bits (32-bit unsigned int) |
0 à 4294967295 |
| int32 |
Entier signé 32 bits (32-bit signed int) |
-2147483648 à 2147483647 |
| uint64 |
Entier non signé 64 bits (64-bit unsigned int) |
0 à 18446744073709551615 |
| int64 |
Entier signé 64 bits (64-bit signed int) |
-9223372036854775808 à 9223372036854775807 |
Prenons uint8 et int8 comme exemples. Ce sont tous deux des entiers de 8 bits et peuvent représenter 256 valeurs. Dans le type entier non signé uint8, la plage qu'il peut représenter va de 0 à 255, tandis que dans le type entier signé int8, la plage qu'il peut représenter va de -128 à 127.
En plus des 8 types ci-dessus, il existe trois autres types d'entiers spéciaux, uint, int et uintptr, où uint et int peuvent représenter différentes plages sur différentes plateformes, et uintptr est utilisé pour stocker les adresses de pointeur.
| Type | Plage (Range) the current environment is %d bits\n", unsafe.Sizeof(a)*8)
var b int8 = 125
// Use the %d placeholder in fmt.Printf to output the value of the integer
// Use the %T placeholder in fmt.Printf to output the type of the variable
fmt.Printf("The value of b is %d, and the type is %T\n", b, b)
// Integer operations
// Declare integers c and d, and calculate their sum
c, d := 2, 3
fmt.Printf("c + d = %d\n", c+d)
// 10 - 5
fmt.Printf("10 - 5 = %d\n", 10-5)
// 8 * 10
fmt.Printf("8 * 10 = %d\n", 8*10)
// Example of uintptr usage - storing an address
var ptr uintptr
x := 42
// Convert the pointer to x to uintptr
ptr = uintptr(unsafe.Pointer(&x))
fmt.Printf("The address of x stored in ptr is: %v\n", ptr)
}
After executing the program, we get the following output:
```bash
go run integer.go
The type int in the current environment is 64 bits
The value of b is 125, and the type is int8
c + d = 5
10 - 5 = 5
8 * 10 = 80
The address of x stored in ptr is: 824634818784
Explanation of the output:
The type int in the current environment is 64 bits: This line shows that the int type on the system where the code is being run is 64 bits (or 8 bytes), indicating it is an int64. The unsafe.Sizeof(a) returns the size of variable a in bytes, and multiplying by 8 converts it to bits. This means that the int type can hold larger integer values.
The value of b is 125, and the type is int8: Here, we have declared a variable b of type int8 and assigned it the value 125. The output confirms this by showing both the value and the data type.
c + d = 5, 10 - 5 = 5, 8 * 10 = 80: These lines showcase basic integer arithmetic operations: addition, subtraction, and multiplication. The output confirms the correct results of these calculations.
The address of x stored in ptr is: 824634818784: This demonstrates how uintptr can be used to store a memory address. We're converting a pointer to the integer variable x to a uintptr type. The actual address value will vary each time the program runs. This is an advanced use case typically found in unsafe operations and system programming.
In this file, the unsafe.Sizeof() function can be used to obtain the number of bytes occupied by the current variable type. 1 byte (byte) is equal to 8 bits, so unsafe.Sizeof()*8 can obtain the number of bits occupied by the type. From the output, we can see that the online environment is 64 bits. The actual type of int in the online environment is int64.
We can use the following command in the terminal to determine the current system architecture:
dpkg --print-architecture
amd64