The integer data types of Go language include the following:
Signed: int, int8, int16, int32, Int64, run
Unsigned: uint,uint8,uint16,uint32,uint64,byte
Details are as follows:
data type | With or without symbol | Occupy storage | Value range |
int8 | Signed | Occupy 1 byte, 8 bits | Value range: (-2 to the 7th power) -128~127 (2 to the 7th power -1) |
int16 | Signed | 2 bytes, 16 bits | Value range: (-2 to the 15th power) -32768~32767 (2 to the 15th power -1) |
int32 | Signed | Occupy 4 bytes, 32 bits | Value range: (-2 to the 31st power) -2147483648~2147483647 (2 to the 31st power -1) |
int64
|
Signed | Occupy 8 bytes, 64 bits | Value range: (-2 to the 63rd power) -9223372036854775808~9223372036854775807 (2 to the 63rd power -1) |
uint8 | Unsigned | Occupy 1 byte, 8 bits | Value range: 0~255 (power 8 of 2 -1) |
uint16 | Unsigned | 2 bytes, 16 bits | Value range: 0~65535 (16th power of 2 * -1) |
uint32 | Unsigned | Occupy 4 bytes, 32 bits | Value range: 0~4294967295 (32nd power of 2 -1) |
uint64 | Unsigned | Occupy 8 bytes, 64 bits | Value range: 0~18446744073709551615 (64th power of 2 -1) |
int | Signed |
32-bit system occupies 4 bytes, 32 bits 64 bit system occupies 8 bytes, 64 bits |
GO language integer default data type Value range: (-2 to the 31st power) -2147483648~2147483647 (2 to the 31st power -1) Value range: (-2 to the 63rd power) -9223372036854775808~9223372036854775807 (2 to the 63rd power -1) |
uint | Unsigned |
32-bit system occupies 4 bytes, 32 bits 64 bit system occupies 8 bytes, 64 bits |
Value range: 0~4294967295 (32nd power of 2 -1) Value range: 0~18446744073709551615 (64th power of 2 -1) |
rune | Signed | Occupy 4 bytes, 32 bits |
Value range: (-2 to the 31st power) -2147483648~2147483647 (2 to the 31st power -1) Equivalent to int32, representing a Uincode value |
byte | Unsigned | Occupy 1 byte, 8 bits |
Value range: 0~255 (power 8 of 2 -1) Equivalent to uint8, byte is used when storing non Chinese characters |
1 package main 2 3 // Reference multiple packages at once 4 import ( 5 "fmt" 6 "unsafe" 7 ) 8 9 func main() { 10 // Go The integer of the language is as follows: 11 12 // Signed integer 13 // int8 Occupy 1 byte, 8 bits Signed Value range:(-2 To the power of 7)-128~127(2 The seventh power of-1) 14 // int16 2 bytes, 16 bits Signed Value range:(-2 To the 15th power of)-32768~32767(2 To the 15th power of-1) 15 // int32 Occupy 4 bytes, 32 bits Signed Value range:(-2 To the 31st power of)-2147483648~2147483647(2 To the 31st power of-1) 16 // int64 Occupy 8 bytes, 64 bits Signed Value range:(-2 To the 63rd power of)-9223372036854775808~9223372036854775807(2 To the 63rd power of-1) 17 var i8 int8 = -128 18 // var i8 int8 = -129 // cannot use -129 (untyped int constant) as int8 value in variable declaration (overflows) 19 // var i8 int8 = 128 // cannot use 128 (untyped int constant) as int8 value in variable declaration (overflows) 20 var i16 int16 = -32768 21 // var i16 int16 = -32769 // cannot use -32769 (untyped int constant) as int16 value in variable declaration (overflows) 22 var i32 int32 = 2147483647 23 // var i32 int32 = 2147483648 // cannot use 2147483648 (untyped int constant) as int32 value in variable declaration (overflows) 24 var i64 int64 = 9223372036854775807 25 // var i64 int64 = 9223372036854775808 // cannot use 9223372036854775808 (untyped int constant) as int64 value in variable declaration (overflows) 26 fmt.Println("i8=", i8) // i8= -127 27 fmt.Println("i16=", i16) // i16= 128 28 fmt.Println("i32=", i32) // i32= 2147483647 29 fmt.Println("i64=", i64) // i64= 2147483648 30 fmt.Println("") 31 32 // Unsigned integer 33 // uint8 Occupy 1 byte, 8 bits Unsigned Value range: 0~255(2 To the 8th power of-1) 34 // uint16 2 bytes, 16 bits Unsigned Value range: 0~65535(2 To the 16th power of*-1) 35 // uint32 Occupy 4 bytes, 32 bits Unsigned Value range: 0~4294967295(2 To the 32nd power of-1) 36 // uint64 Occupy 8 bytes, 64 bits Unsigned Value range: 0~18446744073709551615(2 To the 64th power of-1) 37 var ui8 uint8 = 255 38 // var ui8 uint8 = 256 // cannot use 256 (untyped int constant) as uint8 value in variable declaration (overflows) 39 var ui16 uint16 = 65535 40 // var ui16 uint16 = 65536 // cannot use 65536 (untyped int constant) as uint16 value in variable declaration (overflows) 41 var ui32 uint32 = 4294967295 42 // var ui32 uint32 = 4294967296 // cannot use 4294967296 (untyped int constant) as uint32 value in variable declaration (overflows) 43 var ui64 uint64 = 18446744073709551615 44 // var ui64 uint64 = 18446744073709551616 // cannot use 18446744073709551616 (untyped int constant) as uint64 value in variable declaration (overflows) 45 fmt.Println("ui8=", ui8) // ui8= 255 46 fmt.Println("ui16=", ui16) // ui16= 65535 47 fmt.Println("ui32=", ui32) // ui32= 4294967295 48 fmt.Println("ui64=", ui64) // ui64= 18446744073709551615 49 fmt.Println("") 50 51 // Other common integers int uint rune byte 52 // int Signed 32-bit system 4 bytes, 32 bits Value range:(-2 To the 31st power of)-2147483648~2147483647(2 To the 31st power of-1) 53 // 64 Bit system 8 bytes, 64 bits Value range:(-2 To the 63rd power of)-9223372036854775808~9223372036854775807(2 To the 63rd power of-1) 54 // uint Unsigned 32-bit system 4 bytes, 32 bits Value range: 0~4294967295(2 To the 32nd power of-1) 55 // 64 Bit system 8 bytes, 64 bits Value range: 0~18446744073709551615(2 To the 64th power of-1) 56 // rune Signed 4 bytes, 32 bits Value range:(-2 To the 31st power of)-2147483648~2147483647(2 To the 31st power of-1),equivalence int32,Represents a Uincode value 57 // byte Unsigned 1 byte, 8 bits Value range: 0~255(2 To the 8th power of-1),equivalence uint8,Used when storing non Chinese characters byte 58 var i int = 9223372036854775807 59 // var i int = 9223372036854775808 // cannot use 9223372036854775808 (untyped int constant) as int value in variable declaration (overflows) 60 var ui uint = 18446744073709551615 61 // var ui uint = 18446744073709551616 // cannot use 18446744073709551616 (untyped int constant) as uint value in variable declaration (overflows) 62 var ru rune = 2147483647 63 // var ru rune = 2147483648 // cannot use 2147483648 (untyped int constant) as rune value in variable declaration (overflows) 64 var b byte = 255 65 // var b byte = 256 // cannot use 256 (untyped int constant) as byte value in variable declaration (overflows) 66 fmt.Println("i=", i) // i= 9223372036854775807 67 fmt.Println("ui=", ui) // ui= 18446744073709551615 68 fmt.Println("ru=", ru) // ru= 2147483647 69 fmt.Println("b=", b) // b= 255 70 fmt.Println("") 71 72 // Go Integer considerations for languages: 73 // 1 Integer types are divided into signed and unsigned types, where int and uint The storage size of is related to the system; 74 // 2 The default type of integer is int 75 // 3 Go When the language uses integer type, the data type that occupies less storage space should be selected as far as possible on the premise of meeting the scope of use; 76 // 4 bit Is the smallest storage unit in a computer, byte Is the basic storage unit in the computer, 1 byte = 8 bit; 77 // 5 Go How to view the byte size and data type of variables in the language is as follows 78 var unknownInt int64 = 123 79 fmt.Printf("variable unknownInt occupy%d Bytes of type%T", unsafe.Sizeof(unknownInt), unknownInt) 80 }
Go language integer considerations:
Integers are divided into signed and unsigned types. The storage size of int and uint depends on the system;
2 the default type of integer is int
3 when the go language uses integer types, try to select data types that occupy less storage space on the premise of meeting the scope of use;
4 bit is the smallest storage unit in the computer, byte is the basic storage unit in the computer, and 1 byte = 8 bit;
5 how to view the byte size and data type of variables in go language is as follows
var unknownInt int64 = 123 fmt.Printf("variable unknownInt occupy%d Bytes of type%T", unsafe.Sizeof(unknownInt), unknownInt)
The variable unknownInt takes 8 bytes and the type is int64