A ready-to-use Go template with DI, logging, configuration, database, HTTP server, observability, testing, and Docker support.
This template provides a solid foundation for building Go applications with:
- Dependency Injection: Clean, testable architecture with proper DI container
- Structured Logging: Both structured and unstructured logging with Zap
- Configuration Management: Environment-based configuration with sensible defaults
- Database Integration: PostgreSQL and Kafka support with interfaces
- HTTP Server: Gin-based REST API with health checks and Swagger docs
- Observability: OpenTelemetry tracing and Prometheus metrics
- Testing: Comprehensive test coverage with proper mocking
- Docker Support: Production-ready containerization
- Go 1.21+
- Docker (optional, for databases and monitoring)
- Make (for build automation)
The application uses environment variables with defaults:
# Application
GO_APP_APP_NAME=myapp
GO_APP_ENV=development
GO_APP_PORT=8080
# Logging
LOGGER_WRITE_STDOUT=true
LOGGER_ENABLE_STACK_TRACE=false
LOGGER_APP_LOG_PATH=./logs/app.log
LOGGER_ERR_LOG_PATH=./logs/error.log
goapp/
├── api/ # HTTP layer
│ ├── handlers/ # HTTP handlers with DI
│ └── routes/ # Route definitions
├── cmd/ # Application entrypoints
│ ├── goapp/ # Main HTTP server
├── internal/ # Internal packages (not importable)
│ ├── config/ # Configuration management
│ ├── container/ # Dependency injection container
│ ├── db/ # Database implementations
│ │ ├── postgres/ # PostgreSQL client
│ │ └── kafka/ # Kafka client
│ ├── logging/ # Logging implementation
│ └── observability/ # OpenTelemetry setup
├── pkg/ # Public packages (reusable)
│ └── clients/ # HTTP clients
└── docs/ # Swagger documentation
- pkg/ vs internal/:
pkg/
contains reusable libraries,internal/
contains app-specific code - Interface-driven: All major components implement interfaces for easy testing
- Dependency Injection: No global state, all dependencies are injected
- Configuration: Single source of truth with environment variable support
- Error Handling: Proper error propagation with context
The logging package supports both structured and unstructured logging:
// Structured logging (recommended)
logger.Info("User created",
logging.String("user_id", "123"),
logging.String("email", "user@example.com"))
// Unstructured logging (for simple cases)
logger.Infof("User %s created with email %s", userID, email)
// Context logging
userLogger := logger.With(logging.String("user_id", userID))
userLogger.Info("Processing user request")
PostgreSQL integration with proper error handling:
// The database interface is injected into handlers
func (h *Handler) CreateUser(c *gin.Context) {
// Use h.Database.DB() to get *sqlx.DB instance
user := &User{}
err := h.Database.DB().Get(user, "SELECT * FROM users WHERE id = $1", userID)
if err != nil {
h.Logger.Error("Failed to fetch user", logging.Error(err))
return
}
}
Built-in health checks that verify:
- Application status
- Database connectivity
- External service availability
Type-safe configuration with validation:
type AppConfig struct {
Name string `envconfig:"APP_NAME" default:"goapp"`
Env string `envconfig:"ENV" default:"development"`
Port int `envconfig:"PORT" default:"8080"`
}
# Build for current platform
go build ./cmd/goapp
# Build for Linux
GOOS=linux go build ./cmd/goapp
# Build with race detector
go build -race ./cmd/goapp
# Run all tests
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests with race detector
go test -race ./...
# Build image
docker build -t myapp .
# Run with docker-compose
docker-compose up
- Update
internal/config/config.go
with new configuration - Add the service interface to your package
- Update
internal/container/container.go
to initialize the new service - Inject into handlers via the container
- Create handler methods in
api/handlers/
- Update
api/routes/routes.go
to register routes - Add Swagger documentation comments
- Generate docs:
swag init
// In routes.go
router.Use(authMiddleware(container))
router.Use(corsMiddleware())
- Set
GO_APP_ENV=production
for optimized builds - Use proper secret management for sensitive configuration
- Set up log aggregation (ELK stack, Fluentd, etc.)
- Configure reverse proxy (nginx, Traefik)
- Set up monitoring and alerting
- Use database migrations for schema changes
- Configure graceful shutdown timeouts appropriately
The template includes:
- Prometheus metrics: Available at
/metrics
- OpenTelemetry tracing: Distributed tracing support
- Health checks: Kubernetes-ready health endpoints
- Structured logging: JSON formatted logs for aggregation
This template is provided as-is for creating new Go projects. Modify as needed for your use case.