Go: Data Types: Basic Types
Note: Read the offical references to get the most updated information.
Data Types: Basic Types
Basic Types:
booleans: bool
text: string
numbers:
- int int8 int16 int32 int64
- uint uint8 uint16 uint32 uint64 uintptr # unsigned
- float32 float64
- complex64 complex128
alias:
- byte # alias for uint8
- rune # alias for int32, represents a Unicode code point
Type inference
When declaring a variable without specifying an explicit type, the variable’s type is inferred from the value on the right hand side.
var i int
j := i // j is an int
fmt.Printf("j is of type %T\n", j)
- Numeric constants are high-precision values. An untyped constant takes the type needed by its context.
Booleans
Booleans in Go are represented by the predeclared boolean type bool, which values can be either true or false.
var closed bool // boolean variable 'closed' implicitly initialized with 'false'
speeding := true // boolean variable 'speeding' initialized with 'true'
hasError := false // boolean variable 'hasError' initialized with 'false'
Integer Numbers
The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. uintptr is an unsigned integer large enough to store the uninterpreted bits of a pointer value.
Floating-point numbers
A floating-point number is a number with zero or more digits behind the decimal separator.
Different floating-point types can store different numbers of digits after the digit separator - this is referred to as its precision.
Go has two floating-point types:
-
float32: 32 bits (~6-9 digits precision). -
float64: 64 bits (~15-17 digits precision). This is the default floating-point type.
By default, Go will use float64 for floating-point numbers, unless the floating-point number is:
- assigned to a variable with type
float32, or - returned from a function with return type
float32, or - passed as an argument to the
float32()function.
runes
The rune type in Go is an alias for int32. Given this underlying int32 type, the rune type holds a signed 32-bit integer value. However, unlike an int32 type, the integer value stored in a rune type represents a single Unicode character.
Unicode and Unicode Code Points
Unicode is a superset of ASCII that represents characters by assigning a unique number to every character. This unique number is called a Unicode code point. Unicode aims to represent all the world’s characters including various alphabets, numbers, symbols, and even emoji as Unicode code points.
In Go, the rune type represents a single Unicode code point.
The following table contains example Unicode characters along with their Unicode code point and decimal values:
| Unicode Character | Unicode Code Point | Decimal Value |
|---|---|---|
| 0 | U+0030 |
48 |
| A | U+0041 |
65 |
| a | U+0061 |
97 |
| ¿ | U+00BF |
191 |
| π | U+03C0 |
960 |
| 🧠 | U+1F9E0 |
129504 |
UTF-8 is a variable-width character encoding that is used to encode every Unicode code point as 1, 2, 3, or 4 bytes. Since a Unicode code point can be encoded as a maximum of 4 bytes, the rune type needs to be able to hold up to 4 bytes of data. That is why the rune type is an alias for int32 as an int32 type is capable of holding up to 4 bytes of data.
Go source code files are encoded using UTF-8.
Using Runes
Variables of type rune are declared by placing a character inside single quotes:
myRune := '¿'
Since rune is just an alias for int32, printing a rune’s type will yield int32:
myRune := '¿'
fmt.Printf("myRune type: %T\n", myRune)
// Output: myRune type: int32
Similarly, printing a rune’s value will yield its integer (decimal) value:
myRune := '¿'
fmt.Printf("myRune value: %v\n", myRune)
// Output: myRune value: 191
To print the Unicode character represented by the rune, use the %c formatting verb:
myRune := '¿'
fmt.Printf("myRune Unicode character: %c\n", myRune)
// Output: myRune Unicode character: ¿
To print the Unicode code point represented by the rune, use the %U formatting verb:
myRune := '¿'
fmt.Printf("myRune Unicode code point: %U\n", myRune)
// Output: myRune Unicode code point: U+00BF
Leave a Comment