Packages, export, and internal
Visibility in Go is decided by the case of a letter, and the internal directory closes the API to other modules.
A package is all the files of one directory with the same package clause. Names that start with a capital letter are exported: other packages can see them. User and Parse are visible from the outside. user and parse are not. There is neither public nor private in the language. This is not cosmetic. An unexported name cannot be mentioned even in another package's tests: a test in package user_test sees the package the same way foreign code does.
An import is the module path plus the directory path:
import "example.com/hello/internal/config"
The package name used in code comes from package config, not from the last segment of the path, although they almost always match. If the package was named util and the directory helpers, reading imports becomes painful. Do not do that without a very short reason.
The internal directory is a special rule of the tooling. A package inside example.com/hello/internal/... can be imported only by packages whose path starts with example.com/hello/. The neighboring module example.com/other cannot obtain this import: the build fails. This is how you say "this is not a public library" without waiting for a major version.
Several files in one package share a scope. The function parse from parse.go is visible in server.go of the same package without an import. A cyclic import between packages is forbidden. If httpapi imports store, then store cannot import httpapi. A cycle is a signal that the boundary was drawn in the wrong place: the shared part usually moves into a third package, or into an interface on the caller's side.
How this looks in a service
A typical skeleton, and this platform is built the same way:
cmd/server/main.go
internal/httpapi
internal/store
internal/content
main only wires the dependencies and starts the process. The policy of who may import whom lives in internal and in the direction of the imports, not in a verbal agreement.
A small package with a clear name is better than a large utils. The name util says nothing at the call site. config, store, and runner do.
Practice
What is visible outside
The package
storedeclaresOpen,db, andUser. What can another package call?- All three names, because they are in the same module
- Only Open and User
- Only what lives in the file export.go
- Nothing, until you write public in front of the names
The internal directory
The module is named
example.com/billing. Mark everything that is true of the packageexample.com/billing/internal/store.- It can be imported by example.com/billing/internal/http
- It can be imported by the module example.com/other
- It can be imported by example.com/billing/cmd/api
- Capitalized names inside it stop being exported
An import cycle
Package
httpapiimportsstore. What happens ifstorestarts to importhttpapi?- Nothing. Internal packages may refer to each other however they like
- The build will not pass: an import cycle is forbidden
- The program will build, but it will crash on the first call
- Go will automatically move the shared code into the package internal/cycle