Go repository for learning and testing golang projects.
HelloGo/
├── hello/ # Main Go module with examples
│ └── morestrings/ # String utilities package
├── neural_net_iris/ # ML neural network (gonum)
├── README.md
└── AGENTS.md # Agent instructions
# Clone and setup
go mod download
# Build
go build ./...
# Run
go run ./hello
# Test
go test -v -race ./...Run these before building/testing to ensure clean state:
# Clear module cache (use after go.mod changes)
go clean -modcache
# Clear build cache
go clean -cache
# Clear both
go clean -modcache -cache
go clean -cache -modcache# 1. Clear caches
go clean -modcache -cache
# 2. Tidy dependencies
go mod tidy
# 3. Verify module
go mod verify
# 4. Build
go build -v ./...
# 5. Run tests with race detector
go test -race -v ./...# Initialize new module
go mod init github.com/user/project
# Add dependency
go get github.com/user/package
# Update all dependencies
go get -u ./...
# Remove unused dependencies
go mod tidy
# Download all modules
go mod download
# Verify module integrity
go mod verify
# Vendor dependencies
go mod vendor# Build current module
go build -v ./...
# Build specific package
go build -v ./hello
# Build with version info
go build -ldflags "-X main.Version=1.0.0" ./...
# Cross-compile
GOOS=linux GOARCH=amd64 go build -o bin/server ./...
# Build for multiple platforms
GOOS=linux GOARCH=amd64 go build -o bin/app-linux-amd64 ./...
GOOS=darwin GOARCH=amd64 go build -o bin/app-darwin-amd64 ./...
GOOS=windows GOARCH=amd64 go build -o bin/app-windows-amd64.exe ./...# Run main package
go run .
# Run specific file
go run ./hello/main.go
# Run with race detector
go run -race ./...
# Run with environment variables
DEBUG=true go run .# Run all tests
go test ./...
# Run with verbose output
go test -v ./...
# Run with race detector
go test -race ./...
# Run specific test
go test -v -run TestReverse ./...
# Run benchmarks
go test -bench=. ./...
# Run benchmarks with memory stats
go test -bench=. -benchmem ./...
# Generate coverage report
go test -coverprofile=coverage.out ./...
go tool cover -html=coverage.out
# Run tests in specific directory
go test -v ./hello/...
go test -v ./neural_net_iris/...# Format code
go fmt ./...
# Run go vet
go vet ./...
# Run linter (if installed)
golangci-lint run ./...
# Check for unused imports
goimports -w .# CPU profile
go test -cpuprofile=cpu.out -bench=.
go tool pprof cpu.out
# Memory profile
go test -memprofile=mem.out -bench=.
go tool pprof mem.out# View cache size
go env GOCACHE
# Clear download cache
go clean -modcache
# Clear build cache
go clean -cache
# Clear test cache
go clean -testcache
# Clear everything
go clean -cache -modcache -testcache
# Disable cache (for CI)
GOCACHE=off go build ./...# Create directory
mkdir -p newproject/cmd/newproject
cd newproject
# Initialize module
go mod init github.com/user/newproject
# Create main.go in cmd/
go mod tidy
go build -v ./...# Make code changes, then:
go clean -cache -modcache
go mod tidy
go vet ./...
go build -v ./...
go test -race -v ./...go get github.com/user/package@latest
go mod tidy
go mod verify
go build -v ./...
go test -v ./...go clean -cache -modcache
go build -v -ldflags "-s -w" ./...
./binarycd hello
go build -v ./...
go run .
go test -v ./...
go test -v ./morestrings/...cd neural_net_iris
go build -v -o neural_net_iris .
./neural_net_iris
go test -v -race ./...
go test -bench=. -benchmem ./...| Action | Command |
|---|---|
| Build | go build -v ./... |
| Run | go run . |
| Test | go test -race -v ./... |
| Test (fast) | go test -race ./... |
| Benchmarks | go test -bench=. -benchmem ./... |
| Lint | go vet ./... |
| Format | go fmt ./... |
| Tidy | go mod tidy |
| Clean cache | go clean -cache -modcache |
| Full rebuild | go clean -cache -modcache && go mod tidy && go build -v ./... |
Go 1.26.2 required (per go.mod files).
- Always use
-raceflag when running tests to detect race conditions - Run
go clean -cacheafter dependency changes - Use
go mod tidyto manage dependencies - Run
go vet ./...before committing - Check
go.modfor required Go version
Instead of using package main for everything, create reusable packages:
mkdir -p myproject/pkg/mypackage
cd myproject// pkg/mypackage/package.go
package mypackage
import "context"
type Service struct {
name string
}
func NewService(name string) *Service {
return &Service{name: name}
}
func (s *Service) DoSomething(ctx context.Context) error {
// logic here
return nil
}// internal/ packages are private to the module
myproject/
├── cmd/
│ └── server/
│ └── main.go # Entry point
├── internal/
│ └── mypackage/ # Private package
│ └── package.go
└── go.mod// internal/mypackage/package.go
package mypackage
// Unexported type (private)
type service struct{}
// Exported constructor
func New() *service {
return &service{}
}// mylib/myfunc.go
package mylib
import "errors"
func DoSomething() error {
if false {
return errors.New("failed")
}
return nil
}// mylib/myfunc_test.go
package mylib
import "testing"
func TestDoSomething(t *testing.T) {
if err := DoSomething(); err != nil {
t.Errorf("DoSomething() failed: %v", err)
}
}// cmd/server/main.go
package main
import (
"myproject/internal/mypackage"
)
func main() {
svc := mypackage.New()
// use service
}myproject/
├── cmd/
│ ├── server/
│ │ └── main.go
│ └── cli/
│ └── main.go
├── pkg/
│ ├── models/
│ │ └── user.go
│ └── utils/
│ └── helpers.go
├── internal/
│ ├── auth/
│ │ └── jwt.go
│ └── db/
│ └── postgres.go
├── go.mod
└── README.md
| Package | Purpose |
|---|---|
cmd/ |
Executable entry points |
pkg/ |
Public libraries (importable externally) |
internal/ |
Private to this module |
internal/auth/ |
Authentication logic |
internal/db/ |
Database layer |
internal/api/ |
API handlers |
internal/service/ |
Business logic |
cd hello
mkdir -p morestrings// morestrings/reverse.go
package morestrings
func Reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}// morestrings/reverse_test.go
package morestrings
import "testing"
func TestReverse(t *testing.T) {
tests := []struct {
input string
want string
}{
{"hello", "olleh"},
{"abc", "cba"},
{"a", "a"},
{"", ""},
}
for _, tt := range tests {
t.Run(tt.input, func(t *testing.T) {
got := Reverse(tt.input)
if got != tt.want {
t.Errorf("Reverse(%q) = %q, want %q", tt.input, got, tt.want)
}
})
}
}// Using the package
package main
import (
"fmt"
"github.com/gluppler/hello/morestrings"
)
func main() {
fmt.Println(morestrings.Reverse("hello")) // prints: olleh
}// internal/handler/user.go
package handler
import "context"
type UserService interface {
GetUser(ctx context.Context, id string) (*User, error)
}
type handler struct {
svc UserService
}
func New(svc UserService) *handler {
return &handler{svc: svc}
}package mypackage
// Exported (public) - starts with uppercase
func NewService() *Service {
return &Service{}
}
// Unexported (private) - starts with lowercase
type service struct{}// Test in same package to access unexported functions
package mypackage
import "testing"
func TestPrivateFunction(t *testing.T) {
// Can test unexported things
result := unexportedHelper()
if result != expected {
t.Errorf("got %v, want %v", result, expected)
}
}# Build specific package
go build -v ./pkg/mypackage
go build -v ./internal/mypackage
# Test package
go test -v ./pkg/mypackage/...
go test -v ./internal/...
# Test all packages
go test -race -v ./...// Using relative path within module
import "github.com/user/project/internal/mypackage"| Action | Command |
|---|---|
| Create package | mkdir -p pkg/mypackage |
| Create internal | mkdir -p internal/mypackage |
| Create cmd entry | mkdir -p cmd/myapp |
| Build package | go build -v ./pkg/mypackage |
| Test package | go test -v ./pkg/mypackage |
| Test all | go test -race -v ./... |