Namespaces¶
A namespace is the unit within which a private declaration may be used. By default each file is its own namespace, named after the file.
| File | Namespace |
|---|---|
user_repository.go |
userRepository |
user_repository_test.go |
userRepository. A test shares the namespace of its subject |
parser_linux.go |
parser. A GOOS or GOARCH suffix is a build constraint, not a namespace |
user_id.go, parse_json.go |
userID, parseJSON. An initialism is spelled the way Go spells it |
foo-bar.go, Foo.go |
fooBar, foo. Any separator, and a PascalCase stem, become lowerCamelCase |
2fa_auth.go |
2faAuth. A valid namespace, but never a prefix |
The namespace comes from the file name and is not equal to it. So you can rename a file without renaming what it declares.
Files join a shared namespace with a directive before the package clause. This is how one unit spans several files. The name must be an unexported identifier.
The core namespace¶
One namespace in a package may be its core: the unit the package is named for. Several files may carry //declscope:core, and they share the one core namespace.
The core has no prefix, so the naming rule asks nothing of its declarations. No prefix still names exactly one unit.
Important
//declscope:core sets the namespace and nothing else. It carries no scope. A core declaration still takes defaults.unexported: by default it is private to the core, and a file outside the core that names it crosses a boundary.
| Written on a file | Effect |
|---|---|
//declscope:core |
The file joins the core. What it declares keeps the default scope |
//declscope:core and //declscope:package |
Both apply. The file is core, and what it declares is package-wide |
//declscope:core and //declscope:namespace |
Refused. A core file's namespace is the core |