Standard Go Project Structure
About 899 wordsAbout 3 min
Standard Go Project StructureGo
2025-02-12
A standard Go project follows a modular, maintainable, and clear structure, ideal for small to medium applications or microservices. Below is a brief overview and related knowledge
1. Project Structure
While Go doesn’t enforce a strict structure, the community recommends the following layout, leveraging Go Modules:
text
myproject/
├── go.mod # Module definition with dependencies
├── go.sum # Dependency checksums
├── main.go # Program entry point
├── cmd/ # Executable commands or entry points
│ └── api/ # Example: API service entry
│ └── main.go
├── internal/ # Private packages, restricted from external access
│ ├── config/ # Configuration loading logic
│ ├── models/ # Data models (e.g., structs)
│ └── services/ # Business logic
├── pkg/ # Reusable public packages
│ └── util/ # Utility functions
├── api/ # API-related (e.g., OpenAPI specs, routes)
├── scripts/ # Build/deployment scripts
├── test/ # Test-related files
├── Dockerfile # Docker configuration (optional)
└── README.md # Project documentation
Key Files and Directories
go.mod
- This file defines the module path and its dependencies. It is used by Go tools to resolve dependencies and verify the consistency of the module.go.sum
- This file contains the checksums of the module dependencies. It is used by Go tools to verify the integrity of the downloaded dependencies.main.go
- This file contains the entry point of the program. It is the main file that is executed when the program is run.cmd/
- This directory contains the executable commands or entry points of the program. Each command is defined in its own subdirectory, with its ownmain.go
file.internal/
- This directory contains private packages, which are not intended to be used outside the project. It is a good practice to keep the internal packages isolated from external access, to avoid potential conflicts or security issues.pkg/
- This directory contains reusable public packages. It is a good practice to keep the public packages small and focused, with a clear purpose and well-defined interface.api/
- This directory contains API-related files, such as OpenAPI specifications and routes.scripts/
- This directory contains build/deployment scripts, such as Dockerfiles and Makefiles.test/
- This directory contains test-related files, such as test cases and test fixtures.Dockerfile
- This file contains the configuration for building a Docker image of the program. It is optional, but it is recommended to use it to simplify the deployment process.README.md
- This file contains the project documentation, including a brief description of the project and instructions for running and testing the program.
2. Modules
Go modules are the primary mechanism for managing dependencies in Go. A module is a collection of Go packages that are versioned together and share a common module path. Each module has its own go.mod
file, which defines the module path and its dependencies. The go.sum
file contains the checksums of the module dependencies.
To create a new module, run the following command in the project directory:
go mod init <module path>
For example, to create a module for a project named myproject
, run:
go mod init github.com/myuser/myproject
3. Dependency Management
Go modules are designed to manage dependencies in a consistent and reproducible way. The go.mod
file defines the module path and its dependencies, and the go.sum
file contains the checksums of the module dependencies.
To add a new dependency to the project, run the following command:
go get <package path>
For example, to add the github.com/pkg/errors
package, run:
go get github.com/pkg/errors
To update all dependencies to their latest versions, run:
go get -u
4. Go Modules and Semantic Versioning
Go modules use semantic versioning, which means that each version of a package is assigned a unique identifier in the format vX.Y.Z
, where X
is the major version, Y
is the minor version, and Z
is the patch version.
- Major version changes: These changes are not backwards-compatible and may include breaking changes to the API.
- Minor version changes: These changes are backwards-compatible and may include new features or bug fixes.
- Patch version changes: These changes are backwards-compatible and include only bug fixes.
5. Go Modules and Go Versioning
Go modules are designed to work with the latest Go version. When a new Go version is released, the Go team updates the go.mod
file to use the new version. This ensures that the module works with the latest Go version, without requiring any changes to the module.
6. Go Modules
- Introduced in Go 1.11, replacing $GOPATH.
- Initialize: go mod init github.com/username/myproject.
- Add dependency: go get github.com/gorilla/mux.
- Clean dependencies: go mod tidy.
7. Code Organization
- Single Responsibility: Each package focuses on one function.
- Naming: Use lowercase, concise package names; files reflect functionality (e.g., server.go).
- Error Handling: Explicitly check errors (e.g., if err != nil).
8. Tools
- Testing: Run unit tests with go test; test files end in _test.go.
- Formatting: Use go fmt for consistent style.
- Static Analysis: golangci-lint for code quality checks.
- Dependency Injection: Use wire or manual injection.
- Build: go build for compiling, supports cross-compilation.
9. Common Libraries
- Web Frameworks: Gin, Echo, Beego, Gorilla, Martini, Revel.
- Database Drivers: MySQL, PostgreSQL, MongoDB, Redis.
- JSON Parsing: Use json.Unmarshal or json.NewDecoder.
- Logging: Use zerolog or logrus.
- Configuration: Use viper or cobra.
- Testing: Use Ginkgo or GoConvey.
10. Best Practices
- Error Wrapping: Use errors.Wrap or similar for context.
- Modularity: Keep packages small and focused.
- Testing: Aim for high test coverage.
- Documentation: Use README.md and code comments for clarity.