iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Get Started

Install Go, write your first program, build a binary, and ship it. The complete first-week loop.

Go — getting started

EXAMPLE
# ===== 1. Install =====
# macOS:
brew install go
# Linux:
# Download from go.dev/dl, extract to /usr/local/go, add to PATH:
export PATH=$PATH:/usr/local/go/bin
# Windows:
winget install GoLang.Go

# Verify:
go version
# go version go1.22.x linux/amd64

# ===== 2. Workspace =====
mkdir hello && cd hello
go mod init example.com/hello

# main.go
package main

import "fmt"

func main() {
    fmt.Println("hello, Go")
}

# Run:
go run .

# Build:
go build -o hello
./hello

# ===== 3. Adding a dependency =====
go get github.com/google/uuid
# In code:
import "github.com/google/uuid"
id := uuid.NewString()

go mod tidy        # clean up go.mod / go.sum

# ===== 4. A tiny HTTP server =====
package main

import (
    "encoding/json"
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("content-type", "application/json")
        json.NewEncoder(w).Encode(map[string]any{"ok": true})
    })
    log.Println("listening :8080")
    log.Fatal(http.ListenAndServe(":8080", nil))
}

go run .

# ===== 5. Testing =====
# main_test.go
package main

import "testing"

func TestAdd(t *testing.T) {
    if 1 + 1 != 2 {
        t.Fatal("math is broken")
    }
}

go test ./...

# ===== 6. Cross-compile =====
GOOS=linux   GOARCH=amd64 go build -o hello-linux
GOOS=darwin  GOARCH=arm64 go build -o hello-mac
GOOS=windows GOARCH=amd64 go build -o hello.exe

# ===== 7. Layout (small project) =====
# .
# main.go
# go.mod
# go.sum
# cmd/         binaries
# internal/    private packages
# pkg/         public-ish packages (optional)

# ===== Patterns to internalise =====
# - One module per repo (go.mod at the root)
# - gofmt + go vet on save
# - context.Context as the first param of any I/O function
# - Errors as values; if err != nil { return err }

# ===== Pitfalls =====
# - Missing go.mod -> 'cannot find module' errors
# - Spaces instead of tabs (gofmt enforces)
# - Capturing loop variables (fixed in Go 1.22+; appears in older code)
# - Shadowing err with :=

Why it matters

Install, init a module, write main.go, go run + go build, add tests. That loop is all you need for the first week. Cross-compile to ship a single binary anywhere; that is the Go superpower.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# install from https://go.dev/dl, then:
go version
go run hello.go
Try it Yourself »

Discussion

Loading…