Are metadata or attributes allowed in the Golang?

How these various validation libraries add this metadata to structures such as:

type Post struct {
    Title    string `valid:"alphanum,required"`
    Message  string `valid:"duck,ascii"`
    AuthorIP string `valid:"ipv4"`
    Date     string `valid:"-"`
}

I am confused, Title property, type - string. Other than this, how can you simply add valid:"alphanum,required"Is this reflection used?

Are similar attributes in other languages?

[Required]
public int Title { get;set; }
+4
source share
2 answers

Go has no attributes in a general sense. The lines in the structure are the structure tags :

, . , .

// A struct corresponding to the TimeStamp protocol buffer.
// The tag strings define the protocol buffer field numbers.
struct {
    microsec  uint64 "field 1"
    serverIP6 uint64 "field 2"
    process   string "field 3"
}

, , reflect.

, , - " ",

// +build amd64

//go:noinline

, IIRC .

+3

. ( Struct):

, . , .

.

, doc:

A StructTag - struct.

, , : "". , (U + 0020 ''), quote (U + 0022 '"') (U + 003A ':'). U + 0022 '' ' Go.

.

json. json package docs:

- , . "json" - , .

.

.

+2

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


All Articles