How to Structure a Large Go Application

Organize large Go apps using cmd for entry points, pkg for public libraries, and internal for private code.

Structure a large Go application by organizing code into a cmd directory for entry points and a pkg or internal directory for shared libraries, following the standard Go project layout. Place your main application logic in cmd/myapp/main.go and reusable packages in pkg/ or internal/ to enforce visibility rules.

mkdir -p cmd/myapp pkg/utils internal/config
touch cmd/myapp/main.go pkg/utils/helper.go internal/config/settings.go
  1. Create the cmd directory for application entry points and add your main file. mkdir -p cmd/myapp && touch cmd/myapp/main.go
  2. Create the pkg directory for public libraries that other projects can import. mkdir -p pkg/utils && touch pkg/utils/helper.go
  3. Create the internal directory for private libraries that only your application can import. mkdir -p internal/config && touch internal/config/settings.go
  4. Initialize the module to manage dependencies and enforce the directory structure. go mod init myapp
  5. Build the application from the root to verify the structure compiles correctly. go build ./cmd/myapp