Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Micro Go Template

A minimal, opinionated scaffold for Go microservices and web apps. Use this as a starting point for new projects. It provides a clean directory layout, placeholders for common layers, and folders for configs, templates, static files, and logs.

Module name (from go.mod): github.com/dariubs/micro — update this to your own module path when you start a new project.

Directory layout

micro/
├─ app/
│  ├─ exe/        # Executables (entrypoints). Put your `package main` here.
│  ├─ handler/    # Transport layer (HTTP/RPC) handlers/controllers.
│  ├─ model/      # Domain and data access models.
│  ├─ types/      # Shared types, DTOs, response/request structs.
│  └─ util/       # Utilities and helpers (logging, errors, etc.).
├─ config/        # Configuration files (YAML/JSON/TOML) and env examples.
├─ static/        # Public/static assets (css, js, images, fonts).
├─ view/          # Server-side templates if using HTML rendering.
├─ log/           # Log output directory (git-ignored in real projects).
├─ build/         # Optional build artifacts/out directory.
└─ go.mod         # Go module definition (currently set to Go 1.15).

Each empty folder includes a .keep file so the directory structure is tracked in version control.

Quick start (use this template)

  1. Create a new project from this template (copy or fork), then update the module path:
# In the project root
$Env:GO111MODULE = "on"
go mod edit -module github.com/youruser/yourproject
# Optionally update the Go version in go.mod
# Example: set to 1.22 (recommended)
go mod edit -go=1.22

go mod tidy
  1. Add an entrypoint in app/exe/main.go:
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, "ok")
    })

    fmt.Println("listening on :8080")
    _ = http.ListenAndServe(":8080", nil)
}
  1. Run it:
go run ./app/exe
  1. Build it:
go build -o build/app.exe ./app/exe

Recommended conventions

  • Packages: Keep package main only in app/exe. All other code should be library packages.
  • Handlers in app/handler should be thin; push business logic into app/model or dedicated services.
  • Types in app/types are shared DTOs; avoid circular dependencies with model and handler.
  • Utilities in app/util should be generic and reusable.
  • Configuration: Load from config/ and/or environment variables. Consider providing a config.example.yaml.
  • Logging: Write to stdout in development; optional file sink to log/ in production (rotate logs externally).

Updating Go version

The template uses go 1.15 in go.mod. Update to a modern version for new projects:

go mod edit -go=1.22

Ensure your local toolchain matches:

go version

Next steps

  • Choose an HTTP router/framework (e.g., standard library, Gin, Echo, Fiber) and add it under app/handler.
  • Add configuration loading (Viper or env parsing) and define config structs.
  • Implement application logging (zap, zerolog, slog) in app/util.
  • Add CI, tests, and a release workflow.

License

Add your preferred license (MIT, Apache-2.0, etc.) to LICENSE.

About

My golang apps base template

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors