tag 是用來提供 struct 中更多資訊的,只能在reflection中取得
What are the use(s) for tags in Go?
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. The tags are made visible through a reflection interface but are otherwise ignored.
// 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" }
GopherCon 2015 had a presentation about struct tags called:
The Many Faces of Struct Tags (slide) (and a video)
json - used by the encoding/json package, detailed at json.Marshal()xml - used by the encoding/xml package, detailed at xml.Marshal()bson - used by gobson, detailed at bson.Marshal(); also by the mongo-go driver, detailed at bson package docprotobuf - used by github.com/golang/protobuf/proto, detailed in the package docyaml - used by the gopkg.in/yaml.v2 package, detailed at yaml.Marshal()db - used by the github.com/jmoiron/sqlx package; also used by github.com/go-gorp/gorp packageorm - used by the github.com/astaxie/beego/orm package, detailed at Models – Beego ORMgorm - used by gorm.io/gorm, examples can be found in their docsvalid - used by the github.com/asaskevich/govalidator package, examples can be found in the project pagedatastore - used by appengine/datastore (Google App Engine platform, Datastore service), detailed at Propertiesschema - used by github.com/gorilla/schema to fill a struct with HTML form values, detailed in the package doc