Stage 1 · Code
Functions & Composite Types
Defining Functions
Write functions that take inputs, produce outputs, and keep your code organized into reusable, readable pieces.
Function Syntax
A function bundles a sequence of statements under a name so you can reuse them. In Go, every function starts with the func keyword, followed by the name, parameters in parentheses, optional return types, and a body in braces.
In Go there are no classes. Functions, methods on types, and interfaces are how you organize behaviour. Start with plain functions; add methods and interfaces when the design calls for them.
Parameters and Arguments
Parameters are the names declared in the function signature. Arguments are the values passed in at the call site. Go passes all basic types and structs by value — the function gets a copy, not the original.
Return Values
A function with no return type returns nothing — it is called for its side effects. A function can return early from any point in its body, not only at the end.
Package-Level Visibility
In Go, capitalized names are exported (visible outside the package). Lowercase names are unexported (package-private). This rule applies to functions, types, variables, and constants.
| Name | Visible from | Example |
|---|---|---|
| Exported | Any package that imports this one | func Connect(...) — public API |
| Unexported | Only within the same package | func dial(...) — implementation detail |
Functions as Values
Functions in Go are first-class values. You can assign a function to a variable, pass it as an argument, or return it from another function. This enables flexible patterns like callbacks and middleware.
A function should do one thing and do it clearly. If you find yourself writing a long comment to explain what a function does, that is usually a signal to split it into smaller functions with self-documenting names.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.