Define and call a function
#Parameters use name type; the result type follows ). Multiple results are allowed. Omit the result type when nothing is returned.
Fragment
Function declaration, then a call fragment inside main.
Reference
Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.
TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.
Parameters use name type; the result type follows ). Multiple results are allowed. Omit the result type when nothing is returned.
Fragment
Function declaration, then a call fragment inside main.
An executable uses package main with a parameterless main function. Top-level function declarations do not run just because they are declared.
Fragment
Complete main.go.
struct declares fields; the receiver before the method name binds the method to a type. A *Counter receiver can update the original. LinePrinter, Push and Finish are exercise-defined.
Fragment
Type and method declarations at file scope; value statements inside a function.
Go has no class constructor syntax. A struct literal initializes fields; a function named New or NewType is a convention, not a keyword. It may return a value or pointer. The built-in new(T) separately returns a pointer to a zero-valued T.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.
A function literal can close over surrounding variables. It is a value with a function type. Captured variables can outlive the call that created the closure; capture is not necessarily a value snapshot.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.
Go has no default/optional parameters in ordinary function signatures. A final ...T parameter receives a slice. values... expands a slice in a variadic call; it does not create function overloading.
Fragment
Fragment: imports and type/function declarations at file scope; usage statements inside a function.