Anatomy of a Go file
Go is a statically typed, compiled programming language designed for simplicity, concurrency, and performance.
File extensions: .go
Every part of the example below is labelled and explained. This page is one of 55 annotated tours on AnatomyOf, a free, open-source project by LunarWerx Studios.
What is inside a Go file
Package declaration
Defines the package name (e.g., main for executables).
Every Go source file starts with a package clause. Files sharing a package name in the same directory form a single compilation unit that can freely reference each other's identifiers without imports. The special package name main marks an executable rather than a reusable library. A main package must additionally provide a func main(): the combination of package main and func main() is what makes go build produce a runnable binary instead of just compiling importable code.
Comment
Single-line (//) or block (/* ... */), ignored by the compiler.
// comments run to the end of the line; /* ... */ block comments can span multiple lines but do not nest. Both are stripped before compilation and have no effect on the compiled binary. A comment placed directly above a top-level declaration with no blank line in between becomes that identifier's "doc comment": go doc and pkg.go.dev render it as the item's documentation, so convention asks the comment to start with the identifier's own name, e.g. // Area returns ....
Import statement
Brings in external packages.
A parenthesized import ( ... ) block lists one package path per line; a single import can also be written as import "fmt" without parentheses. Every imported package must be used somewhere in the file, or the compiler refuses to build. Go treats unused imports as an error, not a warning. Imported identifiers are referenced through the package name, such as fmt.Printf or math.Pi. Tools like goimports automatically add missing imports and remove unused ones, and group standard-library imports separately from third-party ones by convention.
Struct definition
Defines a custom data type with fields using type ... struct.
A struct groups related fields into a single composite type. type Circle struct { Radius float64 } declares a new named type Circle whose values each carry their own Radius. Structs are Go's primary way to model records, since the language has no classes. Struct values are copied by default on assignment or when passed to a function; pointers (*Circle) are used when shared mutation or avoiding a copy is desired. Struct literals like Circle{Radius: r} construct a value directly, and field names may be omitted if every field is supplied positionally in order.
Field (struct member)
Data stored within a struct.
Each line inside a struct body declares a field: a name followed by its type, such as Radius float64. Fields are accessed with dot notation (c.Radius) and can be read or reassigned directly unless the struct is accessed through an interface that hides them. A field name starting with an uppercase letter, like Radius, is exported and visible to other packages; a lowercase name such as radius would be unexported and package-private. This capitalization rule is Go's only visibility mechanism. There is no public/private keyword.
Method definition (with receiver)
A function associated with a type, declared with a receiver.
Writing func (c Circle) Area() float64 attaches Area to the Circle type through the receiver (c Circle), which sits between func and the method name. Inside the method body, c refers to the specific Circle value the method was called on, similar to self or this in other languages. A value receiver like (c Circle) operates on a copy of the struct, so mutations inside the method do not affect the caller's original; a pointer receiver (c *Circle) operates on the original and is required if the method needs to modify fields or avoid copying a large struct.
Return statement
Exits the function and returns a value.
return immediately ends execution of the current function and sends the given expression back to the caller. A function's return type(s) are declared after its parameter list, such as the float64 in func (c Circle) Area() float64, and every code path through the function must return a value of that type. Go functions can return multiple values, most commonly a result paired with an error, e.g. func divide(a, b float64) (float64, error). Named return values can also be declared in the signature and returned with a bare return, which fills them in from whatever the named variables currently hold
Main function
The entry point for a Go application (func main).
When a program is built from package main, execution begins at func main(). It takes no parameters and returns nothing; the process exits with status 0 when main returns normally, or a nonzero status if the program calls os.Exit with another value or panics. The main package declares one package-level main function. It is syntactically callable like another function, but doing so simply invokes it again and does not create a second entry point. Startup logic such as flag parsing and dependency wiring typically lives in main or in init functions that run before it.
Variable declaration (short)
Declares and initializes variables with :=.
The short variable declaration r := 5.0 declares r and infers its type from the initializer in a single step; it is only valid inside a function body, not at package scope. The longer form var r float64 = 5.0 (or var r float64 with a later assignment) works anywhere and is required when you want a type different from what would be inferred, or a zero-valued variable with no initializer. := can also declare several names at once, such as c := Circle{Radius: r}, and is idiomatic for the common pattern of capturing a function's result alongside its error: area, err := compute(). At least one vari
Method/function call
Executing a function or method.
A call expression like c.Area() looks up the Area method on the value c and executes it, here assigning its single return value to area via :=. Calling a regular function follows the same syntax without a receiver, e.g. add(1, 2). Go evaluates arguments left to right before the call happens, and (because the language has no method overloading) each method or function name resolves to exactly one signature per type. fmt.Printf(...) in the same example is itself a function call, taking a format string and the values to substitute into it.
Control flow (conditional)
Executes code based on a condition (if, else).
if area > 50 { ... } else { ... } branches on a boolean expression; unlike C or Java, the condition needs no parentheses, but the braces are mandatory even for a single statement. if can also include a short init statement, as in if err := doWork(); err != nil { ... }, scoping err to just the if/else chain. The (err != nil) idiom shown there is the standard way Go signals failure: rather than exceptions, most functions return an error as their last value, and callers check it explicitly right after the call. else is optional, and else if chains additional conditions.
Control flow (loop)
Repeats code: for is Go's only loop keyword.
for i := 0; i < 3; i++ { ... } is the classic three-clause form: init, condition, post-statement. Go has no separate while or do...while keyword, dropping the init and post clauses (for condition { ... }) gives while-loop behavior, and omitting everything (for { ... }) produces an infinite loop. for ... range iterates over arrays, slices, strings, maps, and channels, yielding an index/key and value pair each pass. break exits the loop immediately and continue skips to the next iteration; both can target an outer loop when combined with a label.
Function definition
Defines a reusable block of code with func.
A top-level function like func add(a, b int) int { return a + b } declares its parameters and their types, followed by its return type. Consecutive parameters that share a type can drop the repeated annotation, so a, b int means both a and b are int. A capitalized function name such as Add would be exported and callable from other packages that import this one; the lowercase add here is unexported and only reachable from within its own package. Functions are values in Go. They can be assigned to variables, passed as arguments, and returned from other functions.
Official Go site · All languages on AnatomyOf
The interactive tour needs JavaScript. Enable it to hover a callout and trace it into the code.