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.modandgo.sumwith the package version. - The cache is stored in:
- Linux/macOS:
~/go/pkg/mod - Windows:
%USERPROFILE%/go/pkg/mod
- Linux/macOS:
How this differs from Python
| Feature | Python (pip) | Go (go get) |
|---|---|---|
| Install location | Virtual environment or system site-packages | Shared module cache |
| Per-project isolation | Achieved via venv/conda | Achieved via go.mod version tracking |
| Package retrieval | From PyPI | From VCS (GitHub, etc.) or module proxy |
| Version control | requirements.txt | go.mod + go.sum |
| Editable install | pip 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