Go (Golang): Novice to Pro Guide
1. Novice Level (The Basics)
1.1 Introduction
- What is Go? Statically typed, compiled, designed by Google for simplicity and concurrency.
- Setup: Install Go, set
GOPATH (or use Modules), go run main.go.
1.2 Syntax & Variables
- Packages:
package main
- Imports:
import "fmt"
- Variables:
var x int = 10 or short declaration x := 10.
- Types:
bool, string, int, float64.
- Constants:
const Pi = 3.14.
1.3 Control Flow
if, else (no parentheses around condition).
switch (no need for break).
for (the only loop in Go: for i := 0; i < 10; i++, for condition, for (infinite)).
1.4 Functions
func add(x int, y int) int { return x + y }
- Multiple return values:
func swap(x, y string) (string, string)
- Named return values.
2.1 Arrays & Slices
- Arrays: Fixed size
[2]string.
- Slices: Dynamic
[]int. make([]int, 5). append(slice, elem).
- Slicing:
s[1:4].
2.2 Maps
- Key-Value pairs:
make(map[string]int).
delete(m, key).
- Checking existence:
val, ok := m["key"].
2.3 Pointers
& (address of), * (dereference).
- Passing by reference vs value.
2.4 Structs & Methods
type Person struct { Name string; Age int }.
- Methods (receivers):
func (p *Person) Greet().
- No classes, but composition/embedding.
2.5 Interfaces
- Implicit implementation (duck typing).
type geometry interface { area() float64 }.
- Empty interface
interface{} (Any type).
3. Advanced Level (Concurrency & Error Handling)
3.1 Concurrency (Goroutines & Channels)
- Goroutines:
go f(x, y, z). Lightweight threads.
- Channels: Typed conduits.
ch <- v (send), v := <-ch (receive).
- Buffered Channels.
select statement for multiple channels.
sync.Mutex and sync.WaitGroup.
3.2 Error Handling
- No exceptions. Errors are values.
if err != nil { return err }.
- Custom errors (
errors.New).
defer, panic, recover.
3.3 Packages & Modules
go.mod, go.sum.
- Exported names start with Capital letter.
init() function.
4.1 Testing
testing package.
func TestXxx(t *testing.T).
- Benchmarks:
func BenchmarkXxx(b *testing.B).
4.2 Reflection & Unsafe
reflect package (runtime inspection).
unsafe package (careful!).
4.3 Web Development
net/http package.
- Middleware patterns.
- JSON marshalling/unmarshalling (
encoding/json).
gofmt (formatting).
go vet (linting).
- Context package (
context.Context) for cancellation/timeouts.