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 |
parser_test_linux.go |
parserTest. As in go/build, only a name ending in _test.go is a test |
parser_linux.pb.go, parser.x_linux.go |
parserPb, parserXLinux. As in go/build, a suffix counts only before the first dot |
user_id.go, user_ids.go, parse_json.go |
userID, userIDs, parseJSON. An initialism, or its plural, is spelled the way Go spells it |
foo-bar.go, Foo.go |
fooBar, foo. Any separator, and a PascalCase stem, become lowerCamelCase |
HTTPServer.go, URLs.go, IPv4.go |
httpServer, urls, ipv4. A leading initialism is lowered the way Go lowers one |
2fa_auth.go, 用户.go |
2faAuth, 用户. Valid namespaces, but never a prefix: the first character must be a letter with a capital form |
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 an explicitly named 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.
A test file joins the core when it tests a core file, as client_test.go tests client.go. The test file is matched by its whole name, so client_linux_test.go joins a core client_linux.go but not a core client.go. Only a test file joins: a core client_test.go does not take client.go into the core, and a core client_linux.go does not take client.go. A test file that names its own namespace with //declscope:namespace keeps it.
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:shared |
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 |