1. Introduction & use cases
Go (Golang) is an open-source language from Google designed for simple, fast and reliable software. It compiles to a single static binary, has built-in concurrency (goroutines and channels), a strong standard library (HTTP server, JSON, testing) and very fast build times.
Because a Go program is one file with no runtime to install, deployment is simple: build, copy, run. Docker, Kubernetes and Terraform are themselves written in Go.
Where it is used
- High-throughput APIs and microservices
- Cloud-native tools, CLIs and DevOps utilities
- Network services, proxies and real-time systems
- Background workers processing queues
2. Prerequisites
| You need | Version / notes |
|---|---|
| Go | Latest stable release from go.dev/dl (Go supports the two most recent major versions) |
| Editor | VS Code with the official Go extension (installs gopls) |
| Git | Needed to download modules |
3. Local setup
1Install Go
Windows/macOS: run the installer from go.dev/dl. Linux: extract the tarball into
/usr/localand add it to PATH (replace the version with the current one).BASH# Linux wget https://go.dev/dl/go1.XX.X.linux-amd64.tar.gz sudo rm -rf /usr/local/go && sudo tar -C /usr/local -xzf go1.XX.X.linux-amd64.tar.gz echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.profile && source ~/.profile go version2Create a module
A module is a project with a
go.modfile. Use your repository path as the module name.BASHmkdir hello-go && cd hello-go go mod init github.com/you/hello-go
4. Create & run a project
1Write an HTTP server
Since Go 1.22 the standard
ServeMuxsupports methods and path parameters, so no framework is needed for many APIs.main.gopackage main import ( "encoding/json" "log" "net/http" "os" ) func main() { mux := http.NewServeMux() mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]string{"status": "ok"}) }) mux.HandleFunc("GET /hello/{name}", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, " + r.PathValue("name") + "!")) }) port := os.Getenv("PORT") if port == "" { port = "8080" } log.Printf("listening on :%s", port) log.Fatal(http.ListenAndServe(":"+port, mux)) }2Run it
go runcompiles and runs in one step.BASHgo run . curl localhost:8080/hello/world3Add a test
Tests live next to the code in
*_test.gofiles.GO// main_test.go package main import "testing" func TestAdd(t *testing.T) { if 2+2 != 4 { t.Fatal("math is broken") } } // run all tests // go test ./...4Build a binary
One executable, no dependencies.
BASHgo build -o hello-go . ./hello-go
✅ When it works you should see output like Figure 1.
5. Development configuration
Environment variables
Read configuration with os.Getenv (or a small library such as envconfig). Go does not load .env files by itself – set variables in your shell, IDE or systemd unit.
PORT=8080
DATABASE_URL=postgres://go:pass@localhost:5432/app?sslmode=disable
LOG_LEVEL=infoKey dependencies
| Package / tool | Used for |
|---|---|
go.mod / go.sum | Module name, Go version and dependency checksums |
net/http, encoding/json | HTTP server and JSON (standard library) |
database/sql + pgx | PostgreSQL access |
log/slog | Structured logging (standard library) |
golangci-lint | Linter suite |
Folder structure
hello-go/
├── cmd/api/main.go # entry point for larger projects
├── internal/ # private packages
│ ├── handlers/
│ └── store/
├── main.go
├── main_test.go
├── go.mod
└── go.sumEssential commands
| Command | What it does |
|---|---|
go run . | Compile and run |
go build -o app . | Build a binary |
go test ./... | Run all tests |
go mod tidy | Add missing / remove unused dependencies |
go fmt ./... && go vet ./... | Format and check code |
GOOS=linux GOARCH=amd64 go build | Cross-compile for a Linux server |
6. Production deployment
Server & hosting requirements
| Requirement | Recommendation |
|---|---|
| Server | Any Linux VPS – Go binaries need no runtime; 512 MB RAM is often enough |
| Build | Go toolchain on your PC or CI (cross-compile) |
| Service manager | systemd |
| Reverse proxy + SSL | Nginx + Let’s Encrypt (or Caddy) |
Deployment steps
1Cross-compile for Linux
Build on Windows/macOS for the server’s OS and CPU.
CGO_ENABLED=0produces a fully static binary.BASHCGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o hello-go . # ARM servers (AWS Graviton): GOARCH=arm642Prepare an Ubuntu server
Create a VPS (AWS EC2, DigitalOcean, Hostinger VPS, Linode …) with Ubuntu 24.04 LTS, at least 1 vCPU and 1–2 GB RAM. Log in with SSH, update the system and create a non-root user for the app.
BASHssh root@YOUR_SERVER_IP apt update && apt upgrade -y adduser deploy usermod -aG sudo deploy # copy your SSH key to the new user, then log in as deploy rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy exit ssh deploy@YOUR_SERVER_IP3Copy the binary
Upload it to the server and make it executable.
BASHscp hello-go deploy@YOUR_SERVER_IP:/srv/hello-go/ ssh deploy@YOUR_SERVER_IP 'chmod +x /srv/hello-go/hello-go'4Run it as a systemd service
systemd starts the app on boot and restarts it on failure. Save the unit file, then enable it. Logs:
journalctl -u hello-go -f./etc/systemd/system/hello-go.service[Unit] Description=hello-go After=network.target [Service] User=deploy WorkingDirectory=/srv/hello-go EnvironmentFile=/srv/hello-go/.env ExecStart=/srv/hello-go/hello-go Restart=always RestartSec=5 [Install] WantedBy=multi-user.target5Start and enable the service
Reload systemd, start the service now and on every boot, and check that it is active (running).
BASHsudo systemctl daemon-reload sudo systemctl enable --now hello-go sudo systemctl status hello-go journalctl -u hello-go -f6Install Nginx as a reverse proxy
Nginx receives requests on ports 80/443 and forwards them to your app on
127.0.0.1:8080. Replaceexample.comwith your domain./etc/nginx/sites-available/example.comserver { listen 80; server_name example.com www.example.com; location / { proxy_pass http://127.0.0.1:8080; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }7Enable the site and reload Nginx
Link the config into
sites-enabled, test it and reload.nginx -tmust say syntax is ok before you reload.BASHsudo apt install -y nginx sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx8Open only the ports you need
Allow SSH, HTTP and HTTPS through the firewall. Application ports (3000, 8000 …) stay closed to the internet – Nginx forwards traffic to them.
BASHsudo ufw allow OpenSSH sudo ufw allow 'Nginx Full' # ports 80 and 443 (after installing Nginx) sudo ufw enable sudo ufw status9Point your domain to the server
In your domain registrar's DNS panel add an A record for
@(andwww) with the server's public IP address. DNS changes usually appear within minutes but can take up to 24–48 hours. Check withdigor an online DNS checker.OUTPUT# Type Name Value TTL # A @ 203.0.113.10 3600 # A www 203.0.113.10 3600 dig +short example.com10Add free HTTPS with Let’s Encrypt
Certbot gets a free SSL certificate, adds it to the Nginx config and redirects HTTP to HTTPS. Certificates renew automatically (a systemd timer runs twice a day); test renewal once with
--dry-run.BASHsudo apt install -y certbot python3-certbot-nginx sudo certbot --nginx -d example.com -d www.example.com sudo certbot renew --dry-run
🗺 The whole path from your code to the live site is drawn in the deployment diagram.
7. Images & screenshots
Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.
8. Common errors & solutions
go: command not found
Go’s bin folder is not on PATH. Add /usr/local/go/bin (Linux/macOS) and open a new terminal.
go: cannot find main module
Run commands inside the folder with go.mod, or create one with go mod init.
missing go.sum entry for module
Run go mod tidy to download modules and update go.sum.
exec format error on the server
The binary was built for another OS/CPU. Rebuild with the right GOOS/GOARCH (amd64 vs arm64).
Data race or random crashes with goroutines
Shared variables are accessed without locks. Find it with go test -race ./... and use sync.Mutex or channels.
Best practices
- Handle every
error– do not ignore return values. - Set timeouts on
http.Server(ReadTimeout,WriteTimeout). - Use
context.Contextfor cancellation and deadlines. - Run
go vetandgo test -racein CI. - Keep binaries small with
-ldflags="-s -w"; ship them in ascratchor distroless Docker image if you use containers.