Primitive Types
The Go programming language features a variety of primitive types, often referred to as predeclared types. Similar types can be found in other languages, such as boolean
, integer
, float
, and string
.
Some examples (opens in a new tab).
Boolean
The boolean type, which takes logical values, has only two possible values: true
and false
. For instance, conditions in if
and for
statements are logical values, and the ==
and <
comparison operators also yield logical values.
Type | Description | Default Value | Example |
---|---|---|---|
bool | boolean type | false | isVisible := true |
For more examples about boolean types, see here (opens in a new tab).
Numeric Types
Go provides several numeric data types for programmers to work with, allowing storage of integers and floating-point numbers of varying sizes and precision, facilitating flexible handling of numeric data.
Type | Description | Default Value | Example |
---|---|---|---|
int | integer of at least 32 bits | 0 | index := 1 |
int8 | 8-bit integer | 0 | var index int8 = 127 |
int16 | 16-bit integer | 0 | var index int16 = 32767 |
int32 | 32-bit integer | 0 | var index int32 = 2147483647 |
int64 | 64-bit integer | 0 | var index int64 = 9223372036854775807 |
uint | unsigned integer of at least 32 bits | 0 | index := 1 |
uint8 | 8-bit unsigned integer | 0 | var index uint8 = 255 |
uint16 | 16-bit unsigned integer | 0 | var index uint16 = 65535 |
uint32 | 32-bit unsigned integer | 0 | var index uint32 = 4294967295 |
uint64 | 64-bit unsigned integer | 0 | var index uint64 = 18446744073709551615 |
byte | alias for uint8 , fully equivalent | 0 | var index byte = 255 |
float32 | 32-bit floating-point number | 0 | var index float32 = -342.3439 |
float64 | 64-bit floating-point number | 0 | var index float64 = -342.3439 |
complex64 | complex number, equivalent to two float32 values | (0+0i) | index := complex(32.3, 18.9) |
complex128 | complex number, equivalent to two float64 values | (0+0i) | index := complex(128.2323823, 1238.2829932) |
For more examples (opens in a new tab).
String
In Go, a string
is fundamentally a sequence of bytes. When string literals are used in Go, they are encoded using the UTF-8 character encoding system by default. Each Unicode code point represents a unique character or symbol, which can be a single letter, digit, punctuation, or even an emoji. In Go, code points are represented using the rune
type, which is an alias for the int32
type.
Since a string
is a sequence of consecutive characters, you have the ability to access individual characters using rune
values. It's important to note that, similar to the string
type, rune
values in Go are also immutable, meaning their values cannot be changed once they're created.
Type | Description | Default Value | Example |
---|---|---|---|
rune | alias for uint32 , fully equivalent | 0 | var index rune = 4294967295 |
string | character string type, convertible to []byte | "" | text := "hello" |
Further examples here (opens in a new tab).
Using special characters in Go
package main
import (
"fmt"
"unsafe"
)
func main() {
str := "A😊" // UTF-8 encoding: "A" is 1 byte, "😊" is 4 bytes.
// Length of the string in bytes
fmt.Println("String length (in bytes):", len(str)) // Output: 5 (1 byte for "A" + 4 bytes for "😊")
// Size of a byte
var b byte = 'A'
fmt.Println("Size of byte:", unsafe.Sizeof(b)) // Output: 1
// Size of a rune
var r rune = '😊'
fmt.Println("Size of rune:", unsafe.Sizeof(r)) // Output: 4
// Convert string to runes
runes := []rune(str)
fmt.Println("Rune array length:", len(runes)) // Output: 2 (1 rune for "A", 1 rune for "😊")
// Total memory for rune array
fmt.Println("Memory for rune array:", len(runes)*int(unsafe.Sizeof(runes[0]))) // Output: 8
}
Try out the code in your browser (opens in a new tab)
Constants
Constants are expressions assigned with specific values. These are evaluated at compile time, not at runtime. To declare a constant, one assigns a value to a name using the const keyword. The fixed nature of their values ensures they cannot be accidentally reassigned during runtime.
Example here (opens in a new tab).
Literals
In Go, literals are the constant values assigned directly in the code. They are used to represent fixed values of various data types such as numbers, strings, booleans, or complex types. Literals are essential for defining values during initialization and for simple calculations.
package main
import "fmt"
func main() {
var dec = 42 // Decimal
var oct = 075 // Octal
var hex = 0x1A3F // Hexadecimal
var bin = 0b1010 // Binary
fmt.Println(dec, oct, hex, bin)
}
Try out the code in your browser here (opens in a new tab).
Special Type
uintptr
is an unsigned integer type in Go that is used to store the memory address of an object. It is primarily used for low-level memory operations and is often associated with working with the Go runtime, operating system calls, or interacting with unsafe pointers.
Example: Using uintptr
with the unsafe
Package
package main
import (
"fmt"
"unsafe"
)
func main() {
// Example array
array := [3]int{10, 20, 30}
// Get a pointer to the first element
ptr := unsafe.Pointer(&array[0])
// Convert to uintptr and manipulate the memory address
for i := 0; i < len(array); i++ {
// Use uintptr to calculate the address of the next element
elementPtr := (*int)(unsafe.Pointer(uintptr(ptr) + uintptr(i)*unsafe.Sizeof(array[0])))
// Print the value at the calculated address
fmt.Println(*elementPtr)
}
}
Try out the code from the browser here (opens in a new tab).