esc
Type to search across all notes

Go Development

Checking Go Installation

go version

This command checks if Go is installed and displays the version and architecture.

Modules and Package Management

Initialize a new Go module

go mod init sample-go

  • Downloads the package into Go’s module cache (not your project folder).
  • Updates go.mod and go.sum with the package version.
  • The cache is stored in:
    • Linux/macOS: ~/go/pkg/mod
    • Windows: %USERPROFILE%/go/pkg/mod

How this differs from Python

FeaturePython (pip)Go (go get)
Install locationVirtual environment or system site-packagesShared module cache
Per-project isolationAchieved via venv/condaAchieved via go.mod version tracking
Package retrievalFrom PyPIFrom VCS (GitHub, etc.) or module proxy
Version controlrequirements.txtgo.mod + go.sum
Editable installpip install -e .No direct equivalent; use local replace directive

Exported vs Unexported Fields

In Go, only exported fields (starting with an uppercase letter) are accessible outside the package and eligible for reflection-based operations (e.g., JSON unmarshalling).

type Person struct {
    Name string // Exported
    age  int    // Unexported
}

var p Person
json.Unmarshal([]byte(`{"Name":"Alice","age":30}`), &p)
// p.Name = "Alice"
// p.age = 0  (unchanged because it's unexported)

Tips: Use JSON struct tags to control field names:

Name string `json:"full_name"`

Generics and Type Parameters

Functions can have type parameters

func PrintSlice[T any](s []T) {
    for _, v := range s {
        fmt.Println(v)
    }
}

Methods cannot have their own type parameters

❌This will fail

func (p Person) DoSomething[T any](value T) {}

Error:

method must have no type parameters

✅ Instead, put type parameters on the type:

type Box[T any] struct {
    value T
}

func (b Box[T]) Get() T {
    return b.value
}

Miscellaneous Useful Commands

Tidy up dependencies

go mod tidy

Removes unused dependencies and fetches any missing ones.

List all dependencies

go list -m all

Cache location

go env GOMODCACHE

References