Getting Started with Go

A practical introduction to Go for backend developers. Installation, basics, and first program.

Go is a statically typed, compiled language designed for simplicity and efficiency. Here’s how to get started.

Installation

# macOS
brew install go

# Linux
wget https://go.dev/dl/go1.21.5.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.21.5.linux-amd64.tar.gz

Hello World

package main

import "fmt"

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

Run with go run main.go.

Why Go?

  • Simple syntax, fast compilation
  • Built-in concurrency with goroutines
  • Excellent standard library
  • Static binaries, easy deployment

Key Concepts

Variables

var name string = "Gopher"
age := 25  // type inference
const Pi = 3.14159

Functions

func add(a, b int) int {
    return a + b
}

// Multiple returns
func divide(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("division by zero")
    }
    return a / b, nil
}

Structs

type Person struct {
    Name string
    Age  int
}

p := Person{Name: "Alice", Age: 30}

Next Steps