Go (Golang) Programming

Fast, simple, and built for modern systems

What is Go?

Go, also known as Golang, is a statically typed, compiled language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. Released in 2009, Go was created to address challenges in large-scale software development.

With built-in concurrency, fast compilation, and a simple syntax, Go has become the language of choice for cloud infrastructure, microservices, and DevOps tools.

Key Features

  • Built-in concurrency with goroutines and channels
  • Fast compilation to native machine code
  • Simple, minimalist syntax that's easy to learn
  • Excellent standard library for networking and web services
  • Built-in testing and documentation tools

Common Use Cases

Cloud & DevOps

Docker, Kubernetes, Terraform - the infrastructure tools powering modern cloud.

Microservices

Building scalable, high-performance APIs and distributed systems with ease.

CLI Tools

Creating fast, portable command-line applications and system utilities.

Getting Started

Download Go from go.dev and follow the installation instructions. Verify with:

go version

Start with the official Go Tour at go.dev/tour to learn the basics. Go's simplicity means you can become productive quickly. The language's emphasis on convention makes codebases consistent and maintainable.

Installing Go

Download the latest version from go.dev or use your system's package manager:

# macOS (using Homebrew)
brew install go

# Linux
wget https://go.dev/dl/go1.21.0.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.21.0.linux-amd64.tar.gz
export PATH=$PATH:/usr/local/go/bin

Set up your GOPATH (optional in modern Go):

export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

Go Modules

Go modules are the standard way to manage dependencies:

# Initialize a new module
go mod init example.com/myproject

# Add a dependency
go get github.com/gin-gonic/gin

# Update dependencies
go mod tidy

# Verify dependencies
go mod verify

Installing Package for Golang: Cobra

CLI library for building command-line applications

Using Go Modules (recommended):

Initialize module (if not already done):

go mod init myproject

Add package to your code:

import "github.com/spf13/cobra"

Download dependencies:

go mod tidy

Go modules automatically manage dependencies. Just import and run go mod tidy to download.

Using go get:
go get github.com/spf13/cobra

Downloads and adds the package to your go.mod file. Works with Go modules enabled.

Installing executables (go install):

Install specific version:

go install github.com/spf13/cobra@latest

Install specific version tag:

go install github.com/spf13/cobra@v1.2.3

Use go install to compile and install command-line tools to $GOPATH/bin or $HOME/go/bin.

Version management in go.mod:

Specify exact version:

go get github.com/spf13/cobra@v1.2.3

Update to latest:

go get -u github.com/spf13/cobra

View available versions:

go list -m -versions github.com/spf13/cobra

💡 Tips:

  • • View dependencies: go list -m all
  • • Clean unused dependencies: go mod tidy
  • • Vendor dependencies: go mod vendor
  • • Check for updates: go list -u -m all

📦 Common Package Repositories:

  • • GitHub: github.com/user/repo
  • • GitLab: gitlab.com/user/repo
  • • Bitbucket: bitbucket.org/user/repo

Hello World Example

Create your first Go program:

package main

import "fmt"

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

Save this as main.go and run it:

go run main.go

# Or build an executable
go build main.go
./main

Popular Go Packages

Gin

High-performance HTTP web framework

Cobra

Library for creating powerful CLI applications

GORM

Fantastic ORM library for database operations

Viper

Complete configuration solution for Go applications

Gorilla Mux

Powerful URL router and dispatcher

Testify

Testing toolkit with common assertions and mocks

Go Language Basics

Variables and Types

// Explicit declaration
var name string = "Alice"
var age int = 30

// Type inference
var height = 5.6

// Short declaration (inside functions)
isStudent := false

// Multiple variables
x, y, z := 1, 2, 3

Structs and Methods

type Person struct {
    Name string
    Age  int
}

func (p Person) Greet() string {
    return fmt.Sprintf("Hello, I'm %s", p.Name)
}

// Usage
person := Person{Name: "Bob", Age: 25}
fmt.Println(person.Greet())

Error Handling

func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, fmt.Errorf("cannot divide by zero")
    }
    return a / b, nil
}

// Usage
result, err := divide(10, 2)
if err != nil {
    log.Fatal(err)
}
fmt.Println(result)

Goroutines and Channels

// Goroutine
go func() {
    fmt.Println("Running concurrently")
}()

// Channels
ch := make(chan string)

go func() {
    ch <- "Hello from goroutine"
}()

msg := <-ch
fmt.Println(msg)