Backend & Languages

Go (Golang) Setup & Deployment Guide

Compiled, fast and simple – install Go, build an HTTP service with the standard library and ship one binary to production.

Updated Official documentation ↗

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 needVersion / notes
GoLatest stable release from go.dev/dl (Go supports the two most recent major versions)
EditorVS Code with the official Go extension (installs gopls)
GitNeeded to download modules

3. Local setup

  1. 1Install Go

    Windows/macOS: run the installer from go.dev/dl. Linux: extract the tarball into /usr/local and 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 version
  2. 2Create a module

    A module is a project with a go.mod file. Use your repository path as the module name.

    BASH
    mkdir hello-go && cd hello-go
    go mod init github.com/you/hello-go

4. Create & run a project

  1. 1Write an HTTP server

    Since Go 1.22 the standard ServeMux supports methods and path parameters, so no framework is needed for many APIs.

    main.go
    package 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))
    }
  2. 2Run it

    go run compiles and runs in one step.

    BASH
    go run .
    curl localhost:8080/hello/world
  3. 3Add a test

    Tests live next to the code in *_test.go files.

    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 ./...
  4. 4Build a binary

    One executable, no dependencies.

    BASH
    go 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.

.env
PORT=8080
DATABASE_URL=postgres://go:pass@localhost:5432/app?sslmode=disable
LOG_LEVEL=info

Key dependencies

Package / toolUsed for
go.mod / go.sumModule name, Go version and dependency checksums
net/http, encoding/jsonHTTP server and JSON (standard library)
database/sql + pgxPostgreSQL access
log/slogStructured logging (standard library)
golangci-lintLinter suite

Folder structure

project 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.sum

Essential commands

CommandWhat it does
go run .Compile and run
go build -o app .Build a binary
go test ./...Run all tests
go mod tidyAdd missing / remove unused dependencies
go fmt ./... && go vet ./...Format and check code
GOOS=linux GOARCH=amd64 go buildCross-compile for a Linux server

6. Production deployment

Server & hosting requirements

RequirementRecommendation
ServerAny Linux VPS – Go binaries need no runtime; 512 MB RAM is often enough
BuildGo toolchain on your PC or CI (cross-compile)
Service managersystemd
Reverse proxy + SSLNginx + Let’s Encrypt (or Caddy)

Deployment steps

  1. 1Cross-compile for Linux

    Build on Windows/macOS for the server’s OS and CPU. CGO_ENABLED=0 produces a fully static binary.

    BASH
    CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o hello-go .
    # ARM servers (AWS Graviton): GOARCH=arm64
  2. 2Prepare 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.

    BASH
    ssh 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_IP
  3. 3Copy the binary

    Upload it to the server and make it executable.

    BASH
    scp hello-go deploy@YOUR_SERVER_IP:/srv/hello-go/
    ssh deploy@YOUR_SERVER_IP 'chmod +x /srv/hello-go/hello-go'
  4. 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.target
  5. 5Start and enable the service

    Reload systemd, start the service now and on every boot, and check that it is active (running).

    BASH
    sudo systemctl daemon-reload
    sudo systemctl enable --now hello-go
    sudo systemctl status hello-go
    journalctl -u hello-go -f
  6. 6Install Nginx as a reverse proxy

    Nginx receives requests on ports 80/443 and forwards them to your app on 127.0.0.1:8080. Replace example.com with your domain.

    /etc/nginx/sites-available/example.com
    server {
        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";
        }
    }
  7. 7Enable the site and reload Nginx

    Link the config into sites-enabled, test it and reload. nginx -t must say syntax is ok before you reload.

    BASH
    sudo apt install -y nginx
    sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
    sudo nginx -t
    sudo systemctl reload nginx
  8. 8Open 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.

    BASH
    sudo ufw allow OpenSSH
    sudo ufw allow 'Nginx Full'   # ports 80 and 443 (after installing Nginx)
    sudo ufw enable
    sudo ufw status
  9. 9Point your domain to the server

    In your domain registrar's DNS panel add an A record for @ (and www) with the server's public IP address. DNS changes usually appear within minutes but can take up to 24–48 hours. Check with dig or 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.com
  10. 10Add 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.

    BASH
    sudo 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.

terminal$ go run .2026/12/04 10:15:02 listening on :8080$ curl localhost:8080/hello/worldHello, world!$ go test ./...ok github.com/you/hello-go 0.004s
Figure 1: terminal output of the sample Go (Golang) project (section 4).
1go test ./...2Cross-compilebinary3scp to server4systemd service5Nginx + HTTPS
Figure 2: production deployment flow (section 6).
hello-go/├── cmd/api/main.go # entry point for larger projects├── internal/ # private packages│ ├── handlers/│ └── store/├── main.go├── main_test.go├── go.mod└── go.sum
Figure 3: project folder structure (section 5).

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.Context for cancellation and deadlines.
  • Run go vet and go test -race in CI.
  • Keep binaries small with -ldflags="-s -w"; ship them in a scratch or distroless Docker image if you use containers.