整数型 (Integers)
整数 (Integers) は、大きく分けて符号なし整数 (Unsigned integers) と符号付き整数 (Signed integers) の 2 つのカテゴリに分類できます。符号付き整数 (Signed integers) が最も広く使用されています。
符号なし (Unsigned) とは、非負の数 (0 と正の数) のみを表現できることを意味し、符号付き (Signed) の数は、負の数と非負の数の両方を表現できます。
符号なし整数 (Unsigned integers) は、8 ビット、16 ビット、32 ビット、および 64 ビットの 4 つのサイズに分割でき、それぞれ uint8、uint16、uint32、および uint64 で表されます。対応する符号付き整数 (Signed integers) は、int8、int16、int32、および int64 です。次の表は、各型が表現できるさまざまな範囲を示しています。
| 型 (Type) |
説明 (Description) |
範囲 (Range) |
| uint8 |
8 ビット符号なし整数 (8-bit unsigned int) |
0 ~ 255 |
| int8 |
8 ビット符号付き整数 (8-bit signed int) |
-128 ~ 127 |
| uint16 |
16 ビット符号なし整数 (16-bit unsigned int) |
0 ~ 65535 |
| int16 |
16 ビット符号付き整数 (16-bit signed int) |
-32768 ~ 32767 |
| uint32 |
32 ビット符号なし整数 (32-bit unsigned int) |
0 ~ 4294967295 |
| int32 |
32 ビット符号付き整数 (32-bit signed int) |
-2147483648 ~ 2147483647 |
| uint64 |
64 ビット符号なし整数 (64-bit unsigned int) |
0 ~ 18446744073709551615 |
| int64 |
64 ビット符号付き整数 (64-bit signed int) |
-9223372036854775808 ~ 9223372036854775807 |
uint8 と int8 を例にとってみましょう。これらはどちらも 8 ビット整数 (8-bit integers) であり、256 個の値を表現できます。符号なし整数型 (Unsigned integer type) uint8 では、表現できる範囲は 0 から 255 ですが、符号付き整数型 (Signed integer type) int8 では、表現できる範囲は -128 から 127 です。
上記の 8 つの型に加えて、uint、int、および uintptr の 3 つの特別な整数型 (integer types) があります。uint と int はプラットフォームによって異なる範囲を表す場合があり、uintptr はポインタアドレス (pointer address) を格納するために使用されます。
| 型 (Type) |
範囲 (Range) |
uint |
32 ビットシステムでは uint32、64 ビットシステムでは uint64 |
int |
32 ビットシステムでは int32、64 ビットシステムでは int64 |
uintptr |
ポインタアドレス (pointer address) を格納するために使用される符号なし整数型 (Unsigned integer type)。主に unsafe 操作のような低レベルプログラミングで使用されます |
次に、integer.go という名前のファイルを作成して、整数 (integers) の使用法を示します。
cd ~/project
touch integer.go
package main
import (
"fmt"
"unsafe"
)
func main() {
// View the type of int in the current environment
// Declare a as the type int
var a int
// Use unsafe.Sizeof() to output the memory size occupied by the type
fmt.Printf("The type int in 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)
}
プログラムを実行すると、次の出力が得られます。
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
出力の説明:
The type int in the current environment is 64 bits: この行は、コードが実行されているシステムの int 型 (type) が 64 ビット (または 8 バイト) であることを示しています。これは int64 であることを示しています。unsafe.Sizeof(a) は変数 a のサイズをバイト単位で返し、8 を掛けることでビットに変換します。これは、int 型 (type) がより大きな整数値 (integer values) を保持できることを意味します。
The value of b is 125, and the type is int8: ここでは、int8 型 (type) の変数 b を宣言し、それに値 125 を割り当てました。出力は、値とデータ型の両方を示すことで、これを確認します。
c + d = 5, 10 - 5 = 5, 8 * 10 = 80: これらの行は、基本的な整数演算 (integer arithmetic operations) (加算、減算、乗算) を示しています。出力は、これらの計算の正しい結果を確認します。
The address of x stored in ptr is: 824634818784: これは、uintptr を使用してメモリアドレス (memory address) を格納する方法を示しています。整数変数 (integer variable) x へのポインタ (pointer) を uintptr 型 (type) に変換しています。実際のアドレス値 (address value) は、プログラムが実行されるたびに異なります。これは、unsafe 操作やシステムプログラミングで通常見られる高度な使用例です。
このファイルでは、unsafe.Sizeof() 関数を使用して、現在の変数の型 (variable type) が占めるバイト数を取得できます。1 バイト (byte) は 8 ビットに等しいため、unsafe.Sizeof()*8 は、型 (type) が占めるビット数を取得できます。出力から、オンライン環境が 64 ビットであることがわかります。オンライン環境での int の実際の型 (type) は int64 です。
ターミナルで次のコマンドを使用して、現在のシステムアーキテクチャ (system architecture) を確認できます。
dpkg --print-architecture
amd64