1.7 KiB
title | description | date | id | taxonomies | references | license | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Source File Organization | 2024-03-09 | 20240309102252 |
|
|
All rights reserved. |
Each source file of a Go package consists of three parts, in the following order.
- A
package
clause defining the package to which it belongs. - A set of
import
declarations that declare imported packages, if any. - A (possibly empty) set of top-level declarations of constants, types, variables, functions, and methods.
📝 All top-level declarations belong to a
package
, and not to a specific source file where they are declared.
One of the primary purposes of using multiple files would be for organizing the code in a given package
, e.g., for readability, for maintainability, etc.
How the code in a package
is divided into multiple source files is generally of no consequence to Go. One notable exception is the program initialization process. The language specification does not define the precise order. Different build systems may read the source files of a given package in different orders. The standard go toolchain reads the source files of a package
in a lexical order using their file names.
The names introduced through import
declarations, if any, may be referenced in the particular source file only, not across the package. This is referred to as the "source file scope".