Я видел такой код:
type Product struct {
Name string `db:"product_name"`
Id int `db:"id"`
}
type Stocks {
Name string `db:"stock_name"`
Price float `db:"price"`
Type string `db:"type"`
}
Хотя этот код компилируется, как это может быть? Член структуры должен состоять из имени и следующего типа. Однако здесь также есть db:"product_name"
, который является третьим участником.
Как это может быть правдой? Что такое db:"product_name"
с точки зрения Go?
The Go Programming Language Specification
A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored.
Это так называемые структурные теги: golang.org/ref/spec#Struct_types