🔵
Languages & Build

Go (Golang)

Install Go for building CLI tools, Kubernetes operators, and cloud-native apps

🐧 Linux🍎 Mac🪟 Windows
Reviewed: Tested on: Kubernetes 1.29 · Terraform 1.8 · Ubuntu 22.04

What is this?

Go is a fast programming language used to build cloud tools like Docker, Kubernetes, and Terraform.

📥

Step 01

Install Go

(01)Install Go

Linux
1# Official binary
2wget https://go.dev/dl/go1.22.0.linux-amd64.tar.gz
3sudo rm -rf /usr/local/go
4sudo tar -C /usr/local -xzf go1.22.0.linux-amd64.tar.gz
5echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
6echo 'export GOPATH=$HOME/go' >> ~/.bashrc
7source ~/.bashrc
8
9# Ubuntu package
10sudo apt-get install -y golang-go
⚙️

Step 02

Configure Go

(01)Set GOPATH and workspace

Linux
1mkdir -p ~/go/{bin,src,pkg}
2go env -w GOPATH=$HOME/go
3go env -w GO111MODULE=on
4echo 'export PATH=$PATH:$HOME/go/bin' >> ~/.bashrc
5source ~/.bashrc
6go env

(02)Create and build a project

Linux
1mkdir -p ~/projects/hello && cd ~/projects/hello
2go mod init github.com/you/hello
3cat > main.go << 'EOF'
4package main
5import "fmt"
6func main() { fmt.Println("Hello, DevOps!") }
7EOF
8go run main.go
9go build -o hello main.go
10./hello

Step 03

Verify Go

(01)Check version

Linux
1go version
2go env GOPATH GOROOT

Step 04

Manage Go

(01)Install DevOps CLI tools

Linux
1go install github.com/weaveworks/eksctl/latest/cmd/eksctl@latest
2go install sigs.k8s.io/kind@latest
3go install github.com/hasura/graphql-engine/cli/v2@latest
4
5# Tools installed to $GOPATH/bin
6ls ~/go/bin/

📋Config templates

2 YAML templates for Go (Golang). Copy and deploy after setup.

2 ready-to-copy templates. Expand one, copy the YAML, then run the deploy commands.

docker-compose.yml

yaml
1version: "3"
2
3services:
4 app:
5 build: .
6 ports:
7 - "8080:8080"
8 depends_on:
9 - db
10 environment:
11 SERVER_PORT: 8080
12 MONGO_URI: mongodb://db:27017
13 security_opt:
14 - no-new-privileges:true
15
16 db:
17 image: mongo:latest
18 restart: always
19 ports:
20 - "27017:27017"
21 security_opt:
22 - no-new-privileges:true

Dockerfile

dockerfile
1FROM golang:1.22-alpine AS builder
2WORKDIR /app
3COPY go.mod go.sum ./
4RUN go mod download
5COPY . .
6RUN CGO_ENABLED=0 go build -o server .
7
8FROM alpine:3.19
9WORKDIR /app
10COPY --from=builder /app/server .
11EXPOSE 8080
12CMD ["./server"]
📄

Step 01

Run the Stack

(01)Build and start

Linux
1docker compose up -d --build
2docker compose ps
3curl http://localhost:8080/health