Go Complete Reference Index
Select any item below to open its full syntax specification, parameters, and live example.
Type semantics
Addressability&variable; &T{...}
Variables, pointer dereferences, slice elements and fields of addressable structs can be addressed. Map elements cannot. Taking the address of a composite literal is an explicit exception. Example output: '7'.
Basic and constraint-only interfacesinterface { Read() int }; interface { ~int }
Basic interfaces can be ordinary value types. Interfaces containing nonbasic type terms or comparable constraints restrict type parameters but cannot be used as ordinary variable types. Example output: '4 6'.
Constant representabilityvar name T = constant
An integer destination needs an integral value within its range. A floating destination permits rounding to its precision without overflow. A type parameter requires representability for every type in its set. Example output: '255'.
Default constant typesvalue := constant
Untyped boolean, rune, integer, floating, complex and string constants default to bool, rune, int, float64, complex128 and string respectively. The value must fit its default type when that type is required. Example output: 'int float64 int32'.
Methods
Literal
Automatic semicolon insertionif condition {
statement
}
Go inserts semicolons after certain final tokens on a line, including identifiers, literals, return and closing delimiters. Moving the opening brace of an if header onto the next line can make it invalid. Example output: 'ok'.
Composite literalsT{ elements }
Composite literals construct struct, array, slice or map values. Keys represent field names, indices or map keys depending on type. Omitted array or struct elements have zero values. Example output: '[0 7 0] [2 4]'.
Standard library / encoding/csv
Function semantics
Expression
Generics
Constraint intersectionsinterface { ~int | ~int64; String() string }
Each interface element further restricts the type set. A union permits alternatives, while a method requirement intersects those alternatives with the types implementing that method. This constraint-only interface cannot be used as a runtime variable type. Example output: '4'.
Constraint satisfaction and comparabilityfunc Equal[T comparable](a,b T) bool
A type argument usually satisfies a constraint by implementing it. Since Go 1.20, comparable interfaces can satisfy suitable strictly comparable constraints; dynamic values may still cause a comparison panic. Example output: 'true'.
Interfaces
Error handling
Custom error typesfunc (e *Failure) Error() string
An error type implements Error() string. Typed errors can carry structured information rather than forcing consumers to parse text; choose consistent pointer or value receivers and use errors.As for extraction. Example output: 'true 7'.
Deferred argument evaluationdefer f(x)
The function value and argument values are evaluated when the defer executes, while the call itself runs later. A closure can instead read a captured variable at execution time. Deferred calls execute in reverse registration order when the surrounding function returns or unwinds. Example output: '2\n1'.
Deferred changes to named resultsfunc f() (n int) { defer func(){n++}(); return 4 }
Return expressions assign named result variables before deferred functions execute. A deferred closure can modify those variables before the caller receives them. Use this deliberately rather than hiding ordinary return logic. Example output: '5'.
Error return contractsfunc f() (T, error)
The final error result conventionally indicates success with nil. On failure, document whether other results are usable; some APIs return partial data with a nonnil error. Do not assume every nonnil error implies no result. Example output: '0 true'.
Error trees and multiple causeserrors.Join(err1, err2)
A joined error can expose multiple underlying errors. Matching via errors.Is searches the error tree, whereas errors.Unwrap only calls the single-error Unwrap() error method and therefore does not traverse a joined error. Join ignores nil inputs. Example output: 'true true true'.
Error wrapping and matchingfmt.Errorf("context: %w", err)
Wrapping exposes an underlying error to errors.Is and errors.As. Only expose implementation errors as part of an API contract when callers may depend on them. Formatting with %v preserves text but does not wrap the error. Example output: 'true false'.
Struct semantics
Showing 20 of 5270 entries