import (
"encoding/json"
"fmt"
"reflect"
)
type T struct {
f1 string "f one"
f2 string
f3 string `f three 123`
f4, f5 int64 `one:"1" two:"2"blank:""`
Name string `json:"name"` //注意要大寫開頭命名
na int `json:"na"` //小寫開頭命名json會被丟棄
}
func main() {
t := reflect.TypeOf(T{})
f1, _ := t.FieldByName("f1")
fmt.Println(f1.Tag) // f one
f4, _ := t.FieldByName("f4")
fmt.Println(f4.Tag) // one:"1" two:"2"blank:""
f5, _ := t.FieldByName("f5")
fmt.Println(f5.Tag) // one:"1" two:"2"blank:"" 共用 同上
v, ok := f5.Tag.Lookup("one")
fmt.Printf("%s, %t\n", v, ok) //1, true
tt := T{"1", "0", "2", 3, 6, "kiki", 6}
b, err := json.Marshal(tt)
if err != nil {
panic(err)
}
fmt.Printf("%s\n", b) //{"name":"kiki"} 只有json tag會被解析出來
}
type Who struct {
Id primitive.ObjectID `json:"id" bson:"_id,omitempty"` //mongo ObjectId
Sub `bson:",inline"` // inline stored in mongo
Docs string `json:"docs,omitempty"`
Name string `json:"name" bson:"name" example:"mira"`
IsActive *bool `json:"isActive,omitempty"`
}
type Sub struct {
Name string `json:"name" bson:"name" example:"name"`
Labels []string `json:"labels" bson:"labels" example:"123"`
Description string `json:"description" bson:"description" example:"1234567"`
Bar string `minLength:"4" maxLength:"16"`
Baz int `minimum:"10" maximum:"20" default:"15"`
Qux []string `enums:"foo,bar,baz"`
}