Go Reference ยท Keywords and Statements
The struct type
Reference for Go struct types: declaring fields, composite literals, anonymous and embedded fields, tags, zero values, comparison and copying behavior.
A struct is a sequence of named fields, each with a name and a type. Structs group related data into one value and are the main way to model records in Go. A struct type is usually given a name with type, and methods are attached to the named type.
Structs are values: assigning one copies all its fields, and passing one to a function copies it. Use a pointer to share and mutate a single instance. The zero value of a struct has every field set to its own zero value, so many structs are usable without a constructor.
type Point struct { X, Y int } p := Point{X: 3, Y: 4}
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
StructType = "struct" "{" { FieldDecl ";" } "}" . FieldDecl = (IdentifierList Type | EmbeddedField) [ Tag ] . EmbeddedField = [ "*" ] TypeName [ TypeArgs ] . Tag = string_lit .
| Part | Description |
|---|---|
| Field names | Must be unique in the struct. Capitalized names are exported; lowercase names are visible only in the declaring package. The blank identifier _ declares padding that cannot be used. |
| Embedded field | A type name without a field name. Its fields and methods are promoted to the outer struct. The implicit field name is the type name. |
| Tag | An optional string literal, usually raw, read with reflect by packages such as encoding/json. |
Usage notes
- Composite literals. Prefer keyed literals (
Point{X: 1, Y: 2}): they survive field reordering and may omit fields. Positional literals must list all fields in order and break when fields are added. - Comparison. Structs are comparable with
==if all their fields are comparable, and can then be map keys. A struct containing a slice, map or function is not comparable. - Copy semantics. Assigning copies the fields shallowly: a slice or map field in the copy still shares the underlying data with the original.
- Embedding. Promotion lets
outer.Fieldreachouter.Inner.Field. If two embedded types supply the same name at the same depth, using it unqualified is an ambiguity error. Embedding is composition, not inheritance. - Zero value. Design types so the zero value is useful (like
sync.Mutexandbytes.Buffer). - Memory layout. Fields are laid out in order with alignment padding. Ordering large fields first can reduce size.
struct{}has size zero and is used for sets and signals. - Methods. Use pointer receivers for methods that modify the struct or for large structs, and keep a type consistent: all pointer or all value receivers.
- Generic structs. A struct can have type parameters:
type Pair[K comparable, V any] struct { Key K; Val V }.
Related
type,interface,map,func, Type parameters- Lessons: Structs, Embedding, Struct tags, Methods