reflect包的幾個使用場景:json
1. 遍歷結構體字段名(避免代碼的硬編碼)
2. 調用結構體方法(自動映射)
3. 獲取結構體的tag標記的值(json/xml轉換)
4. // @todo更多的使用場景this
代碼:編碼
1、$GOPATH/reflectusage/main.go:code
// reflect使用場景 package main import ( "errors" "fmt" "reflect" "reflectusage/config" ) func main() { // 建立Conf實例 conf := config.Conf{} opName := "create" conf.Op = &opName conf.Port = 3308 fmt.Printf("conf.Port=%d\n\n", conf.Port) // 結構信息 t := reflect.TypeOf(conf) // 值信息 v := reflect.ValueOf(conf) printStructField(&t) callMethod(&v, "SayOp", []interface{}{" Db"}) // panic: reflect: Call of unexported method //callMethod(&v, "getDbConf", []interface{}{}) getTag(&t, "Op", "json") getTag(&t, "Op", "xml") getTag(&t, "nofield", "json") } // 場景1:遍歷結構體字段名 func printStructField(t *reflect.Type) { fieldNum := (*t).NumField() for i := 0; i < fieldNum; i++ { fmt.Printf("conf's field: %s\n", (*t).Field(i).Name) } fmt.Println("") } // 場景2:調用結構體方法 func callMethod(v *reflect.Value, method string, params []interface{}) { // 字符串方法調用,且能找到實例conf的屬性.Op f := (*v).MethodByName(method) if f.IsValid() { args := make([]reflect.Value, len(params)) for k, param := range params { args[k] = reflect.ValueOf(param) } // 調用 ret := f.Call(args) if ret[0].Kind() == reflect.String { fmt.Printf("%s Called result: %s\n", method, ret[0].String()) } } else { fmt.Println("can't call " + method) } fmt.Println("") } // 場景3:獲取結構體的tag標記 func getTag(t *reflect.Type, field string, tagName string) { var ( tagVal string err error ) fieldVal, ok := (*t).FieldByName(field) if ok { tagVal = fieldVal.Tag.Get(tagName) } else { err = errors.New("no field named:" + field) } fmt.Printf("get struct[%s] tag[%s]: %s, error:%v\n", field, tagName, tagVal, err) fmt.Println("") } // @todo更多的使用場景
2、$GOPATH/reflectusage/config/config.go:xml
package config type Db struct { Port int Host string pw string } type Conf struct { Op *string `json:"jsonop" xml:"xmlOpName"` Charlist *string Length *int Num *int Output *string Input *string hidden *string Db } func (this Conf) SayOp(subname string) string { return *this.Op + subname } func (this Conf) getDbConf() Db { return this.Db }
3、輸出:字符串
conf.Port=3308 conf's field: Op conf's field: Charlist conf's field: Length conf's field: Num conf's field: Output conf's field: Input conf's field: hidden conf's field: Db SayOp Called result: create Db get struct[Op] tag[json]: jsonop, error:<nil> get struct[Op] tag[xml]: xmlOpName, error:<nil> get struct[nofield] tag[json]: , error:no field named:nofield