Go is statically typed with a clean, simple type system. There are no classes — Go uses structs and methods for object-oriented programming.
Basic Types
Go
var (
name string = "Sita" // text
age int = 21 // whole number (64-bit on modern systems)
price float64 = 480.50 // decimal (default)
active bool = true // true or false
)| Type | Size | Use for |
|---|---|---|
int |
8 bytes (64-bit) | Most whole numbers |
int8/16/32/64 |
1-8 bytes | When size matters |
uint |
8 bytes | Unsigned whole numbers |
float32/64 |
4-8 bytes | Decimal numbers |
string |
variable | Immutable text (UTF-8) |
bool |
1 byte | true / false |
byte |
1 byte | Alias for uint8 |
rune |
4 bytes | Alias for int32 (Unicode code point) |
Zero Values
Every type has a zero value — the default when no value is given:
Go
var i int // 0
var f float64 // 0.0
var b bool // false
var s string // "" (empty string)Arrays and Slices
Go
// Array — fixed size (rarely used directly)
var arr [5]int
// Slice — dynamic size (the one you'll use most)
nums := []int{1, 2, 3, 4, 5}
nums = append(nums, 6) // add to end
// Make with length and capacity
buf := make([]byte, 0, 1024)| Feature | Array | Slice |
|---|---|---|
| Size | Fixed | Dynamic |
| Declaration | [5]int |
[]int |
| Resizable | ❌ | ✅ (via append) |
| Use when | Size known at compile time | Most cases |
Maps
Go
// Create with literal
prices := map[string]int{
"Notebook": 150,
"Pen": 25,
}
// Add entries
prices["Bag"] = 800
// Safe lookup (comma-ok idiom)
val, ok := prices["Book"] // ok is false if key missing
if ok {
fmt.Println(val)
}
// Delete
delete(prices, "Pen")
// Iterate
for item, cost := range prices {
fmt.Printf("%s: %d\n", item, cost)
}Structs
Go
type Book struct {
Title string
Author string
Pages int
}
// Constructor function (convention: New + type name)
func NewBook(title, author string, pages int) Book {
return Book{Title: title, Author: author, Pages: pages}
}
// Method on the struct
func (b Book) Summary() string {
return fmt.Sprintf("%s by %s (%d pages)", b.Title, b.Author, b.Pages)
}
book := NewBook("Palpasa Cafe", "Narayan Wagle", 312)
fmt.Println(book.Summary())Value vs Pointer Receivers
Go
// Value receiver — gets a copy (cannot modify the struct)
func (b Book) Summary() string { ... }
// Pointer receiver — gets a reference (can modify the struct)
func (b *Book) SetPages(p int) { b.Pages = p }Rule: use pointer receivers if any method needs to modify the struct.
Tips
- Use slices, not arrays, unless you know the exact size.
- Use maps instead of writing your own hash table.
- Structs are Go's replacement for classes — no inheritance, just composition.
- Always check the comma-ok idiom for map lookups.
Next: functions — multiple returns, closures, defer.