Similar to .net attributes in Go

Which is similar to .NET attributes in go lang.

Or how can this be achieved?

+4
source share
1 answer

Perhaps the most similar mechanism is Struct tags. Not the most elegant, but they can be evaluated at runtime and provide metadata for members of the structure.

From reflection package documentation: enter StructTag

They are used, for example, in JSON and XML encoding for user element names.

, json, , , JSON, , , , - , . , :

type Foo struct {
    Bar string `json:"-"` //will not appear in the JSON serialization at all
    Baz string `json:",omitempty"` //will only appear in the json if not empty
    Gaz string `json:"fuzz"` //will appear with the name fuzz, not Gaz
}

API REST, .

" : ", Get StructTag , example.

+7

Source: https://habr.com/ru/post/1542559/


All Articles