Structs
Last updated
Was this helpful?
Last updated
Was this helpful?
Go’s structs are typed collections of fields. They’re useful for grouping data together to form records.
struct являются значениями
При присваивании :=
struct полностью копируется
переменные типа struct не могут иметь ссылку на nil
The number of fields of a struct type can be zero.
var entry Entry
- создаст структуру со значениями равными значению по умолчанию для своего типа, то есть var entry Entry = Entry {Key: "", Value: 0}
.
A struct type can't have a field of the struct type itself, neither directly nor recursively.
Сравнение:
struct
сравниваются по значению. Две структуры считаются равными если они имеют одинаковые поля.
Whether or not a struct is comparable depends on the struct’s fields. Structs that are entirely composed of comparable types are comparable; those with slice or map fields are not (function and channel fields also prevent a struct from being comparable).
If two struct variables are being compared and at least one of them has a type that’s an anonymous struct, you can compare them without a type conversion if the fields of both structs have the same names, order, and types.
Доступ
Struct fields are accessed using a dot.
Structs are mutable.
You can also use dots with struct pointers - the pointers are automatically dereferenced.
You can list just a subset of fields by using the Name: syntax. (And the order of named fields is irrelevant.)
Структура типа S
не может содержать поле такого же типа S
(агрегатное значение не может содержать само себя), но может объявить поле с типом указателя на *S
.
Test tables:
When a struct value is assigned to another struct value, the effect is the same as assigning each field one by one.
Struct types have the ability to contain anonymous or embedded fields. This is also called embedding a type. When we embed a type into a struct, the name of the type acts as the field name for what is then an embedded field.
We have declared a new type called Admin
and embedded the User
type within the struct declaration. This is not inheritance but composition. There is no relationship between the User
and the Admin
type.
The type that is embedded is then called an inner
type of the new outer
type. Through inner type promotion, identifiers from the inner
type are promoted up to the outer
type. These promoted identifiers become part of the outer
type as if they were declared explicitly by the type itself. The outer
type is then composed of every- thing the inner type contains, and new fields and methods can be added. We still can access the inner
type's method directly.
Please note that, a Singer
value is not a Person
value, the following code doesn't compile:
Embedding an interface in a struct automatically defines all of the interface’s methods on the struct. It doesn’t provide any implementations of those methods, so you need to implement the methods that you care about.
Для встроенных структур работает следующее правило: метод, определенный на User
будет так же применим Admin
, поскольку User
встроен в Admin
(обратное неверно):
Переопределение функций работает в Go для разных типов структур:
Полями структур могут быть методы
Anonymous struct types are allowed to be used as the types of the fields of another struct type. Anonymous struct type literals are also allowed to be used in composite literals.