栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Go中解析动态YAML的惯用方式是什么?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Go中解析动态YAML的惯用方式是什么?

这是我想出的。更具可读性。

// 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}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/369202.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号