Getting Started with Go

Golang, or simply “Go,” is often praised for its simplicity, speed, and first-class support for concurrency. It compiles fast, runs fast, and ships as a single binary. Whether you're building cloud-native tools, high-performance APIs, or simple command-line utilities, Go has you covered.

In this article, we'll walk through getting started with Go by writing a basic "Hello, World!" command-line tool and then exposing it as a simple HTTP API that you can curl.


Why Go?

Go has been widely adopted in the DevOps world. Consider tools like Docker, Kubernetes, and Terraform. They're all written in Go. That’s no accident. Go is statically typed, fast to compile, and has a clean standard library, plus, it’s easy to cross-compile for different operating systems.


Step 1: Installing Go

First, install Go from the official site:
👉 https://go.dev/dl/

On macOS with Homebrew:

brew install go

On Linux:

sudo apt-get update
sudo apt-get install golang-go

Confirm installation:

go version

Step 2: Setting Up a Go Project

Create a new directory and initialize your Go module:

mkdir hello-go-example
cd hello-go-example
go mod init github.com/<your github username>/hello-go-example

This creates a go.mod file that tracks dependencies.


Step 3: Hello World – CLI

Create a file called main.go:

package main

import "fmt"

func main() {
    fmt.Println("Hello, Everyday DevOps!")
}

Run it:

go run main.go

Or compile it:

go build -o hello
./hello

You now have a single, portable binary.


Step 4: Hello World – HTTP API

Let’s add a basic web server you can hit with curl.

Create a new file called api.go:

package main

import (
	"fmt"
	"log"
	"net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello from the Go API!")
}

func main() {
	http.HandleFunc("/hello", helloHandler)
	fmt.Println("Starting server on :8080")
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Build and run:

go run api.go

Test it:

curl http://localhost:8080/hello

You should see:

Hello from the Go API!

Bonus: Combine CLI and API

You can detect command-line flags and optionally run the API:

// combined.go
package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
)

func main() {
	api := flag.Bool("api", false, "Run as HTTP API")
	flag.Parse()

	if *api {
		http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
			fmt.Fprintln(w, "Hello from the Go API!")
		})
		fmt.Println("Serving HTTP on :8080")
		log.Fatal(http.ListenAndServe(":8080", nil))
	} else {
		fmt.Println("Hello, Everyday DevOps!")
	}
}

Now:

go run combined.go           # CLI
go run combined.go -api      # API

Add everything to git.

git init

cat << EOF > .gitignore
.env
hello
EOF

git add .
git commit -m "First checkin" -a

Wrapping Up

Go makes it easy to write clean and efficient programs with minimal setup. With just a few lines of code, you’ve got a CLI and an HTTP server ready to go. From here, you can explore packages, test frameworks, and popular tools like cobra for CLI apps or gin for APIs.

In DevOps, simplicity and speed matter. Go delivers both.


I've created a repo for this at: https://github.com/markcallen/hello-go-example