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
- Create the
cmddirectory for application entry points and add your main file.mkdir -p cmd/myapp && touch cmd/myapp/main.go - Create the
pkgdirectory for public libraries that other projects can import.mkdir -p pkg/utils && touch pkg/utils/helper.go - Create the
internaldirectory for private libraries that only your application can import.mkdir -p internal/config && touch internal/config/settings.go - Initialize the module to manage dependencies and enforce the directory structure.
go mod init myapp - Build the application from the root to verify the structure compiles correctly.
go build ./cmd/myapp