这是我想出的。更具可读性。
// Config represents TAXII 2.0 plugin structuretype Config struct { DefaultRoot string APIRoots []model.APIRoot}// Intermediate config for mapstructuretype configRaw struct { DefaultRoot string `mapstructure:"default"` APIRoots map[string]apiRootRaw `mapstructure:"api_roots"`}type apiRootRaw struct { AuthMethods []string `mapstructure:"auth"` Collections map[string]collectionRaw `mapstructure:"collections"`}type collectionRaw struct { Selector string `mapstructure:"selector"` SelectorQueryLang string `mapstructure:"selector_query_lang"`}func parseTaxiiConfig() error { plg.ConfigMutex.Lock() defer plg.ConfigMutex.Unlock() taxiiConfig := plg.ConfigData.Plugins["taxii20"].(map[interface{}]interface{}) fmt.Println(taxiiConfig) ConfigData = &Config{} raw := &configRaw{} err := mapstructure.Depre(taxiiConfig, raw) if err != nil { return err } ConfigData.DefaultRoot = raw.DefaultRoot ConfigData.APIRoots = make([]model.APIRoot, 0) for name, root := range raw.APIRoots { apiRoot := model.APIRoot{Name: name} apiRoot.AuthMethods = root.AuthMethods apiRoot.Collections = make([]model.Collection, 0) for colName, col := range root.Collections { collection := model.Collection{Name: colName} collection.Selector = col.Selector collection.SelectorQueryLang = col.SelectorQueryLang apiRoot.Collections = append(apiRoot.Collections, collection) } ConfigData.APIRoots = append(ConfigData.APIRoots, apiRoot) } return nil}


