A composite literal in Go is an expression that creates a new value of a struct, array, slice or map and fills it in one step, for example Point{X: 1, Y: 2} or []int{1, 2, 3}. It is the idiomatic way to initialise composite data types, and each time it is evaluated it creates a new value.
Composite literals also work with pointers (&Point{1, 2}), can be nested with the inner type omitted, and support generic types since Go 1.18.
A composite literal is a type followed by a brace-enclosed list of elements:
The type can be a struct type, an array type, a slice type, a map type, or a named type whose underlying type is one of those. Elements are separated by commas, and a trailing comma is required when the closing brace is on a new line.
One parsing detail matters: in if, for and switch headers a composite literal of a named type must be wrapped in parentheses, otherwise the opening brace is read as the start of the block.
Struct literals can name the fields (keyed) or list values in declaration order (positional).
Prefer keyed literals. They stay correct when someone adds or reorders fields, and go vet reports unkeyed fields for structs imported from other packages because adding a field later would break your code. A positional literal must supply every field.
An array literal has a length (or ... to let the compiler count) and a slice literal has none.
Arrays are values and their length is part of the type; slices are small headers that point at an underlying array. Indexed elements (1: 100) let you build sparse arrays and lookup tables, and the index must be a constant.
Map literals require a key for every entry and create a non-nil map.
A var m map[string]int is a nil map and writing to it panics, whereas map[string]int{} is ready to use. Duplicate constant keys in a map literal are a compile-time error.
Inside a composite literal you may omit the element type when it is the same as the outer element type, which keeps nested data readable.
For []*Point, the shorthand {5, 6} means &Point{5, 6}. Map keys of composite type can be elided in the same way.
Composite literals are not limited to named types. An anonymous struct literal is handy for table-driven tests and one-off configuration:
With generics (Go 1.18+), you instantiate the generic type in the literal:
Taking the address of a composite literal (&T{}) is the usual replacement for a constructor in simple cases; the compiler decides whether the value lives on the stack or the heap through escape analysis, so you do not need to think about allocation. Since Go 1.21 the built-in clear and min/max work alongside these types, but they do not change how literals are written.
new(T) and &T{}?Both return a pointer to a new value, but new(T) always gives the zero value, while &T{...} lets you set fields at creation. Use &T{} when you want to initialise anything.
Not necessarily. The compiler's escape analysis keeps a value on the stack when it does not outlive the function and moves it to the heap when it does.
go vet complain about unkeyed fields?For struct types from other packages, a positional literal breaks if the maintainers add a field. Keyed literals avoid that, so vet warns.
No. Composite literals are not constant expressions. Declare them with var or inside functions.
Composite literals create and initialise structs, arrays, slices and maps in one expression. Use keyed fields for structs, let ... count array lengths, remember that map literals produce a usable non-nil map, elide repeated inner types when nesting, and wrap named-type literals in parentheses inside if, for and switch headers.