In the Go language, numeric variables are used to store numerical data types, such as integers and floating-point numbers. Go language provides several types of numeric variables, each with different characteristics and representation ranges.
Integer Types#
- int and uint: represent signed integers and unsigned integers, their specific sizes depend on the platform they are on, can be 32-bit or 64-bit.
- int8 and uint8: represent signed 8-bit integers and unsigned 8-bit integers, with ranges of -128 to 127 and 0 to 255 respectively.
- int16 and uint16: represent signed 16-bit integers and unsigned 16-bit integers, with ranges of -32768 to 32767 and 0 to 65535 respectively.
- int32 and uint32: represent signed 32-bit integers and unsigned 32-bit integers, with ranges of -2147483648 to 2147483647 and 0 to 4294967295 respectively.
- int64 and uint64: represent signed 64-bit integers and unsigned 64-bit integers, with ranges of -9223372036854775808 to 9223372036854775807 and 0 to 18446744073709551615 respectively.
Floating-Point Types#
- float32: represents 32-bit floating-point numbers, with a range of approximately ±1.18e-38 to ±3.4e38.
- float64: represents 64-bit floating-point numbers, also known as double, with a range of approximately ±2.23e-308 to ±1.8e308.
byte Type#
- The byte type is an alias for uint8, representing an unsigned integer of one byte in size.
- In Go, the byte type is commonly used to store ASCII characters or binary data.
rune Type#
- The rune type is an alias for int32, representing Unicode characters.
- In Go, the rune type is used to store a single Unicode character and can represent any Unicode code point value.
package main
import "fmt"
func main() {
const char rune = 'A'
fmt.Printf("%T, %d\n", char, char)
// int32, 65
}
Complex Types#
- Go supports complex types for complex number operations, where a complex number consists of a real part and an imaginary part.
- complex64 represents complex numbers with both real and imaginary parts as 32-bit floating-point numbers.
- complex128 represents complex numbers with both real and imaginary parts as 64-bit floating-point numbers.