Packages
A package is a folder of Go source files that compile together and share unexported names. Every .go file in that folder declares the same package name, and other code imports the folder path, not the file.
Detailed Explanation
You are building a small HTTP service. The handler code, the SQL queries, and the token checks do not belong in one file. Go's unit of grouping is the package: a directory of files that the compiler treats as one namespace. Put handler.go and routes.go in internal/httpapi with package httpapi at the top of both, and they can call each other's unexported helpers. A file in cmd/server cannot see those helpers unless you export them.
The import path is the directory, not the package clause. You write import "example.com/shop/internal/httpapi". The last element of that path is usually the package name, but the compiler does not require a match. After the import, you refer to exported names as httpapi.NewServer. If two imports would collide, you rename one at the import site: import bill "example.com/shop/internal/billing".
Visibility is decided by the first letter. A name that starts with an uppercase letter is exported and visible to importers. A lowercase name is package-private. There is no public keyword and no protected. Files in the same package share lowercase names even if they live in different .go files. That is why a package is a folder, not a file: splitting a large package across files does not change who can see what.
main is a special package name. A directory whose files say package main compiles to an executable, and that package must contain func main(). Library packages use any other name. The convention is one package per directory; putting two package names in one folder is a compile error except for _test packages used by tests.
Internal packages are a path rule, not a keyword. A directory named internal can be imported only by a parent of that internal folder. example.com/shop/internal/httpapi is importable from example.com/shop/cmd/server and forbidden from example.com/other. Use it when you want a helper package that is not part of your public API.
Blank and dot imports. import _ "net/http/pprof" runs the imported package's init functions and keeps none of its names. That is how you register database drivers. import . "fmt" dumps exported names into your file; it is almost never worth the confusion.
What it costs. A package boundary is both a compile unit and an API. Too many tiny packages force you to export names you would rather keep private, and create import cycles. One giant package compiles faster to iterate on, but hides the seams. The usual shape is a few medium packages (cmd, internal/..., and maybe one public root) rather than a file-per-type layout copied from Java.
Code Examples
package main
import (
"fmt"
// Import path is the module path plus directories.
"example.com/shop/internal/httpapi"
)
func main() {
s := httpapi.NewServer()
fmt.Println(s)
}File: internal/httpapi/server.go
// File: internal/httpapi/server.go
package httpapi
// NewServer is exported: importers can call it.
func NewServer() *Server {
return &Server{addr: defaultAddr()}
}
// defaultAddr is unexported: only files in package httpapi can call it.
func defaultAddr() string { return ":8080" }
type Server struct {
addr string
}Blank import: run init, keep no names. Drivers register this way.
// Blank import: run init, keep no names. Drivers register this way.
import _ "github.com/lib/pq"Common Gotchas
Import cycles do not compile
if package A imports B and B imports A, the build fails. Move the shared type into a third package, or keep the two files in one package.
The package name and the folder name can differ
you import the path, then use the package clause name. A folder `httpapi` whose files say `package api` is imported as `api.NewServer` after `import "example.com/shop/internal/httpapi"`.
Lowercase names are invisible across packages, including tests in `_test` packages
an external test file with `package httpapi_test` cannot call `defaultAddr`. Put those tests in `package httpapi` if they need internals.
One package per directory
two `package` names in the same folder (other than `foo` and `foo_test`) are a compile error.