A Go package groups related source files in one directory under a shared package name. It organizes applications and provides scope for functions, types, variables, and constants. section 4.1.1
Apply these package rules when structuring Go code:
- Start every Go file with a package declaration, and keep files in the same directory under one package name. section 4.1.1
- Prefer a short, lowercase, usually single-word package name; conventionally match it to the directory name. section 2.1.1 section 4.1.1
- Export a symbol by starting its name with an uppercase letter; lowercase names remain inside the package. section 4.1.1
- Keep exported APIs stable for compatibility while allowing unexported implementation details to change. section 4.1.1
A runnable main package
package main\n\nimport "fmt"\n\nfunc main() {\n fmt.Println("Hello world")\n}\n\nRun it with `go run main.go`. Larger projects can place reusable code in packages such as `gordle`, `pocketlog`, or `genericcache`, alongside `main.go` or at the module root. section 2.1.1 section 5.1 section 7.1.2
Listing 2.1 main.go
package main
import "fmt"
func main() {
fmt.Println("Hello world")
}
Inspect the complete minimal program, then compare its package declaration, import, and main function. section 2.1.1






