Modules, packages and project layout
Until now all your code fit in a single file. Real programs have dozens or hundreds of files, and need a way to be split into parts with clear responsibilities. Go offers two levels of organization. The package groups files in one folder that work together. The module groups one or more packages, has a name and a version, and is the unit that is distributed and from which dependencies can be declared. In this lesson you will see how to create both, how to import your own and others' code and how to structure a project.
The module and go.mod
A module is created with go mod init followed by its path, for example go mod init example.com/shop. That path is the prefix of all internal imports; if you plan to publish the code it is usually the repository URL, like github.com/user/shop. The command generates go.mod, which contains the path, the Go version and the list of dependencies. To add an external library you use go get path, and go mod tidy leaves the file clean: it adds what is missing and removes what is unused. A go.sum file is also created, which stores checksums to guarantee dependencies do not change. Both files are kept in version control.
mkdir shop && cd shop
go mod init example.com/shop
# go.mod now contains:
# module example.com/shop
# go 1.23
go get github.com/google/uuid@latest # add an external dependency
go mod tidy # clean unused / add missing
go list ./... # list all packages of the moduleCreating and using your own package
Every folder with .go files is a package, and all files in the folder declare the same name with package name. Normally the package name matches the folder name. To use it from another package it is imported with the module path plus the folder, and its elements are called with the package name as prefix. Only identifiers starting with an uppercase letter are visible from outside; the rest are internal. Look at the example: the pricing folder contains the logic and main only uses it. Package names must be short, lowercase and without underscores.
// file: pricing/pricing.go
package pricing
const vatRate = 0.21 // private: lowercase
// WithVAT returns the price including VAT. Exported: uppercase.
func WithVAT(net float64) float64 {
return round2(net * (1 + vatRate))
}
func round2(v float64) float64 { // private helper
return float64(int(v*100+0.5)) / 100
}
// file: main.go
// package main
//
// import (
// "fmt"
// "example.com/shop/pricing"
// )
//
// func main() {
// fmt.Println(pricing.WithVAT(100)) // 121
// }Typical project structure
There is no mandatory structure, and small projects can live in a single main package. As they grow, the community uses a convention: the cmd folder with a subdirectory for each executable, and the internal folder for code that must only be used within the module; the compiler prevents other modules from importing what is in internal. Avoid circular dependencies: if package a imports b, then b cannot import a, and Go refuses to compile. If it happens, it usually means you should extract the common part into a third package or into an interface.
shop/
go.mod
go.sum
cmd/
shopd/
main.go # package main: starts the program
internal/
pricing/
pricing.go # package pricing
pricing_test.go
store/
store.go # package store
README.md- go mod init creates the module; go mod tidy keeps it clean.
- One folder = one package; same package name in all its files.
- Leading uppercase exports; lowercase hides.
- internal/ protects code for the module's exclusive use.
- Circular imports are not allowed.
✎ マイノート
このブラウザにのみ保存されます。ログインするとアカウントに保存できます。0/5000 文字