How to Structure a Go Project

Flat vs Layered vs DDD

Structure Go projects using a layered approach with cmd for entry points, internal for private logic, and pkg for public libraries.

Use a layered structure (cmd, internal, pkg) to balance simplicity and maintainability for most Go projects. Place your main entry point in cmd/yourapp/main.go, shared logic in internal/, and reusable libraries in pkg/.

// cmd/yourapp/main.go
package main

import (
	"yourmodule/internal/service"
)

func main() {
	service.Run()
}

// internal/service/service.go
package service

func Run() {
	// Business logic here
}