Go (Golang) Cheatsheet


Setup & Environment

  • Install Go (macOS)
brew install go
  • Install Go (Linux)
# Download latest Go (replace version as needed)
wget https://go.dev/dl/go1.22.2.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.22.2.linux-amd64.tar.gz
# Add to PATH (add to ~/.bashrc or ~/.zshrc)
export PATH=$PATH:/usr/local/go/bin
  • Install Go (Windows)
  1. Download the MSI installer from https://go.dev/dl/
  2. Run the installer and follow the prompts
  3. Add Go to your PATH if not set automatically (usually C:\Go\bin)
  • Check Go version
go version
  • Set up Go environment variables (add to ~/.bashrc or ~/.zshrc)
export GOPATH="$HOME/go"
export PATH="$PATH:$GOPATH/bin"
  • Create a new Go module/project
mkdir myproject && cd myproject
go mod init github.com/username/myproject

Building & Running

  • Run a Go file
go run main.go
  • Build a binary
go build -o myapp main.go
  • Install binary to $GOPATH/bin
go install
  • Clean build cache
go clean

Modules & Dependencies

  • Add a dependency
go get github.com/sirupsen/logrus@latest
  • List dependencies
go list -m all
  • Tidy up go.mod and go.sum
go mod tidy
  • Vendor dependencies
go mod vendor

Testing

  • Run all tests
go test ./...
  • Run tests with verbose output
go test -v ./...
  • Run tests and show code coverage
go test -cover ./...
  • Benchmark tests
go test -bench .

Formatting & Linting

  • Format code
go fmt ./...
  • Vet code (find suspicious constructs)
go vet ./...
  • Lint code (requires golangci-lint)
golangci-lint run

Common Go Commands

  • Download all dependencies
go mod download
  • List available Go tools
go tool
  • Show documentation for a package
go doc fmt
  • Run a Go REPL (requires gore)
gore

Go File Structure

package main

import "fmt"

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

Common Code Patterns

  • Declare a variable
var x int = 10
x := 10 // short form
  • Declare a constant
const Pi = 3.14
  • Define a function
func add(a int, b int) int {
    return a + b
}
  • Define a struct
type User struct {
    Name string
    Age  int
}
  • Create and use a map
m := make(map[string]int)
m["foo"] = 42
  • Create and use a slice
s := []int{1, 2, 3}
s = append(s, 4)
  • For loop
for i := 0; i < 10; i++ {
    fmt.Println(i)
}
  • If/else
if x > 0 {
    fmt.Println("positive")
} else {
    fmt.Println("non-positive")
}
  • Switch
switch day := 3; day {
case 1:
    fmt.Println("Monday")
case 2:
    fmt.Println("Tuesday")
default:
    fmt.Println("Other day")
}
  • Error handling
val, err := strconv.Atoi("123")
if err != nil {
    log.Fatal(err)
}
  • Goroutine (concurrency)
go func() {
    fmt.Println("Hello from goroutine")
}()
  • Channel
ch := make(chan int)
go func() { ch <- 42 }()
val := <-ch

Working with JSON, Files, and Type Conversion

Encoding and Decoding JSON

  • Encode struct to JSON
import (
    "encoding/json"
    "fmt"
)

type User struct {
    Name string `json:"name"`
    Age  int    `json:"age"`
}

user := User{Name: "Alice", Age: 30}
data, err := json.Marshal(user)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Println(string(data)) // {"name":"Alice","age":30}
  • Decode JSON to struct
var user User
jsonStr := `{"name":"Bob","age":25}`
err := json.Unmarshal([]byte(jsonStr), &user)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Printf("%+v\n", user) // {Name:Bob Age:25}

Opening, Reading, and Closing Files

  • Read entire file into string
import (
    "io/ioutil"
    "fmt"
)

content, err := ioutil.ReadFile("file.txt")
if err != nil {
    fmt.Println("error:", err)
}
fmt.Println(string(content))
  • Open file, read line by line, and close
import (
    "bufio"
    "fmt"
    "os"
)

file, err := os.Open("file.txt")
if err != nil {
    fmt.Println("error:", err)
    return
}
defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {
    fmt.Println(scanner.Text())
}
if err := scanner.Err(); err != nil {
    fmt.Println("error:", err)
}

String and Integer Conversion

  • String to int
import (
    "strconv"
    "fmt"
)

s := "123"
i, err := strconv.Atoi(s)
if err != nil {
    fmt.Println("error:", err)
}
fmt.Println(i) // 123
  • Int to string
n := 456
s := strconv.Itoa(n)
fmt.Println(s) // "456"

Useful Packages

  • "fmt" — formatted I/O
  • "os" — OS functions
  • "io/ioutil" — file I/O
  • "net/http" — HTTP client/server
  • "encoding/json" — JSON encoding/decoding
  • "time" — time and date
  • "log" — logging
  • "context" — context for cancellation/timeouts

Go Modules Example (go.mod)

module github.com/username/myproject

go 1.22

require (
    github.com/sirupsen/logrus v1.9.0
)

Resources