You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Go is a statically typed language — every variable has a type known at compile time. This lesson covers Go's basic types, variable declarations, zero values, type inference, constants, and type conversions.
Go provides a rich set of built-in types:
| Type | Size | Range / Description |
|---|---|---|
int8 | 8 bits | -128 to 127 |
int16 | 16 bits | -32,768 to 32,767 |
int32 | 32 bits | -2.1 billion to 2.1 billion |
int64 | 64 bits | -9.2 quintillion to 9.2 quintillion |
int | Platform-dependent | 32 or 64 bits (most common) |
uint8 (byte) | 8 bits | 0 to 255 |
uint16 | 16 bits | 0 to 65,535 |
uint32 | 32 bits | 0 to 4.2 billion |
uint64 | 64 bits | 0 to 18.4 quintillion |
float32 | 32 bits | IEEE-754 single precision |
float64 | 64 bits | IEEE-754 double precision (most common) |
complex64 | 64 bits | Complex number with float32 parts |
complex128 | 128 bits | Complex number with float64 parts |
| Type | Description |
|---|---|
bool | true or false |
string | Immutable sequence of bytes (UTF-8) |
byte | Alias for uint8 |
rune | Alias for int32 — represents a Unicode code point |
error | Built-in interface for error values |
Go offers several ways to declare variables:
var name string = "Alice"
var age int = 30
var active bool = true
var name = "Alice" // inferred as string
var age = 30 // inferred as int
var pi = 3.14 // inferred as float64
name := "Alice"
age := 30
pi := 3.14
The := operator declares and assigns in one step. It can only be used inside functions.
var x, y, z int = 1, 2, 3
// Or with short declaration:
a, b, c := "hello", true, 42
var (
name string = "Alice"
age int = 30
active bool = true
)
In Go, every variable is initialised to its zero value if no value is provided:
| Type | Zero Value |
|---|---|
int, float64, etc. | 0 |
bool | false |
string | "" (empty string) |
| Pointer, slice, map, channel, function, interface | nil |
var count int // 0
var name string // ""
var active bool // false
var data []byte // nil
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.