当前位置: 首页 > news >正文

企业建站系统下载百度app安装下载免费

企业建站系统下载,百度app安装下载免费,做动态网站的用工具栏,中国光刻机最新消息文章博客地址:golang 使用 viper 加载配置 自动反序列化到结构 golang使用 viper 无需设置 mapstructure tag 根据配置文件后缀 自动返序列化到结构解决结构有下划线的字段解析不成功问题 viper 正常加载配置文件 golang viper 其中可以用来 查找、加载和反序列化JSON、TOML…

文章博客地址:golang 使用 viper 加载配置 自动反序列化到结构

  • golang使用 viper 无需设置 mapstructure tag 根据配置文件后缀 自动返序列化到结构
  • 解决结构有下划线的字段解析不成功问题

viper 正常加载配置文件

golang viper 其中可以用来 查找、加载和反序列化JSON、TOML、YAML、HCL、INI、envfile和格式的配置文件

配置文件 test_toml.toml

http_addr = ":8082"
grpc_addr = ":8083"
jaeger_url= "http://localhost:14268/api/traces"
tracing= true

golang代码

type ConfigTest struct {HttpAddr  string `json:"http_addr" toml:"http_addr" yaml:"http_addr"`GrpcAddr  string `json:"grpc_addr" toml:"grpc_addr" yaml:"grpc_addr"`JaegerUrl string `json:"jaeger_url" toml:"jaeger_url" yaml:"jaeger_url" mapstructure:"jaeger_url"`Tracing   bool   `toml:"tracing"  json:"tracing" yaml:"tracing" ` // opentelemetry tracing
}// jaeger 加载配置文件
func TestSourceFile_Unmarshal(t *testing.T) {filePath := "./test_toml.toml"viper.SetConfigFile(filePath)if err := viper.ReadInConfig(); err != nil {t.Error(err)}c := &ConfigTest{}if err := viper.Unmarshal(c); err != nil {t.Error(err)}logger.Infow("Unmarshal file sucess", "v", c)
}

打印返序列化的配置结构

{"level":"info","ts":"2023-08-27T21:35:27.041+0800","caller":"config/source_file_test.go:31","msg":"Unmarshal file sucess","v":{"http_addr":"","grpc_addr":"","jaeger_url":"http://localhost:14268/api/traces","tracing":true}}

可以看到带下划线的字段,不加 mapstructure 标签,是不会反序列化

不加 mapstructure tag实现自动反序列化

查看viper Unmarshal 代码

func (v *Viper) Unmarshal(rawVal interface{}, opts ...DecoderConfigOption) error {return decode(v.AllSettings(), defaultDecoderConfig(rawVal, opts...))
}
func decode(input interface{}, config *mapstructure.DecoderConfig) error {decoder, err := mapstructure.NewDecoder(config)if err != nil {return err}return decoder.Decode(input)
}
func NewDecoder(config *DecoderConfig) (*Decoder, error) {if config.TagName == "" {config.TagName = "mapstructure"}// ...
}
  • 从代码看出 Viper使用的是 github.com/mitchellh/mapstructure来解析值
  • mapstructure 用于将通用的map[string]interface{}解码到对应的 Go 结构体中
  • 默认情况下,mapstructure 使用结构体中字段的名称做这个映射,不区分大小写,比如 Name 字段可以映射到 name、NAME、NaMe 等等
  • 如果没有指定 tagName ,则默认为 mapstructure,这也是为什么带下划线的字段不加 mapstructure 标签无法解析的原因
  • viper 中Unmarshal的第二个是可以指定 DecoderConfigOption 的,从而可以指定 tagName

viper根据文类型件自动解码到结构

  1. 读取文件后缀比如 toml
  2. 根据后缀设置 tagName
  3. 调用 viper.Unmarshal解析
func TestSourceFile_Unmarshal1(t *testing.T) {filePath := "./test_toml.toml"c := &ConfigTest{}if err := viperUnmarshal(c, filePath); err != nil {t.Error(err)}logger.Infow("Unmarshal file sucess", "v", c)
}func viperUnmarshal(v interface{}, configPath string) error {var tagName stringext := filepath.Ext(configPath)if len(ext) > 1 {tagName = ext[1:]}// set decode tag_name, default is mapstructuredecoderConfigOption := func(c *mapstructure.DecoderConfig) {c.TagName = tagName}cViper := viper.New()cViper.SetConfigFile(configPath)if err := cViper.ReadInConfig(); err != nil {return err}return cViper.Unmarshal(v, decoderConfigOption)
}
{"level":"info","ts":"2023-08-27T21:35:34.553+0800","caller":"config/source_file_test.go:40","msg":"Unmarshal file sucess","v":{"http_addr":":8082","grpc_addr":":8083","jaeger_url":"http://localhost:14268/api/traces","tracing":true}}

我已将viper加载配置集成进自己的项目,完整example 代码可以查看 source_file_test.go


文章转载自:
http://dinncophenylene.knnc.cn
http://dinncohaciendado.knnc.cn
http://dinncosuffolk.knnc.cn
http://dinncopollinical.knnc.cn
http://dinncorevivatory.knnc.cn
http://dinncotouchingly.knnc.cn
http://dinncostaminate.knnc.cn
http://dinncocrateriform.knnc.cn
http://dinncoskullfish.knnc.cn
http://dinncomicroprism.knnc.cn
http://dinncoshihkiachwang.knnc.cn
http://dinncoleinster.knnc.cn
http://dinncowidowly.knnc.cn
http://dinncochromograph.knnc.cn
http://dinncoanticlastic.knnc.cn
http://dinncoqueenright.knnc.cn
http://dinncosneer.knnc.cn
http://dinncophotometer.knnc.cn
http://dinncomonal.knnc.cn
http://dinncotimberwork.knnc.cn
http://dinncotenderly.knnc.cn
http://dinncotransliterate.knnc.cn
http://dinncoforebear.knnc.cn
http://dinncofleshy.knnc.cn
http://dinncopreternatural.knnc.cn
http://dinncotrilobate.knnc.cn
http://dinncofictionalist.knnc.cn
http://dinncocontestee.knnc.cn
http://dinncomsee.knnc.cn
http://dinncoconservancy.knnc.cn
http://dinncocalx.knnc.cn
http://dinncohaet.knnc.cn
http://dinncocouncilman.knnc.cn
http://dinncotraffic.knnc.cn
http://dinncosolen.knnc.cn
http://dinncokeratometer.knnc.cn
http://dinncomilage.knnc.cn
http://dinncobouquetiere.knnc.cn
http://dinncosubcollege.knnc.cn
http://dinncoimbower.knnc.cn
http://dinncoglycolytic.knnc.cn
http://dinncoundershirt.knnc.cn
http://dinncogpf.knnc.cn
http://dinncomicrobarograph.knnc.cn
http://dinncobeechy.knnc.cn
http://dinncomonolatrist.knnc.cn
http://dinncogluewater.knnc.cn
http://dinncojodie.knnc.cn
http://dinncoshift.knnc.cn
http://dinncomaidenhair.knnc.cn
http://dinncobidarka.knnc.cn
http://dinncohypertext.knnc.cn
http://dinncorecusant.knnc.cn
http://dinncospringhalt.knnc.cn
http://dinncotristearin.knnc.cn
http://dinncoarchontate.knnc.cn
http://dinncocompeer.knnc.cn
http://dinncoschizonticide.knnc.cn
http://dinncobussbar.knnc.cn
http://dinncobengaline.knnc.cn
http://dinncoisoneph.knnc.cn
http://dinncokatzenjammer.knnc.cn
http://dinncokinetochore.knnc.cn
http://dinncothaumatology.knnc.cn
http://dinncokantar.knnc.cn
http://dinncosinnet.knnc.cn
http://dinncodiproton.knnc.cn
http://dinncothimblewit.knnc.cn
http://dinncokellock.knnc.cn
http://dinncobifurcation.knnc.cn
http://dinncokigali.knnc.cn
http://dinncoterraalba.knnc.cn
http://dinncopuccoon.knnc.cn
http://dinncosavate.knnc.cn
http://dinncoviolinist.knnc.cn
http://dinncopull.knnc.cn
http://dinncogalatia.knnc.cn
http://dinncosubassembly.knnc.cn
http://dinncoenclises.knnc.cn
http://dinncochield.knnc.cn
http://dinncorussia.knnc.cn
http://dinncothio.knnc.cn
http://dinncorubbish.knnc.cn
http://dinncometaphorize.knnc.cn
http://dinncoextensively.knnc.cn
http://dinncoreflectional.knnc.cn
http://dinncoisomerization.knnc.cn
http://dinncohalomethane.knnc.cn
http://dinncodecapod.knnc.cn
http://dinncogalleried.knnc.cn
http://dinncorevenuer.knnc.cn
http://dinncostylopize.knnc.cn
http://dinncomonogram.knnc.cn
http://dinncoarmarian.knnc.cn
http://dinncodropscene.knnc.cn
http://dinncofattypuff.knnc.cn
http://dinncocaught.knnc.cn
http://dinncovalued.knnc.cn
http://dinncoepichlorohydrin.knnc.cn
http://dinncoresolutioner.knnc.cn
http://www.dinnco.com/news/92280.html

相关文章:

  • 汕头网站搜索引擎优化网络营销专业是干什么的
  • 做质粒图谱的网站百度推广开户免费
  • 做网站广告软件二级域名分发平台
  • 柳州市网站制作公司品牌公关
  • 网站服务器结构图seo案例视频教程
  • 网站建设推广浩森宇特深圳搜狗seo
  • 有哪些网站主页做的比较好看百度电脑版官网
  • 教育部学校规划建设发展中心官方网站互联网营销培训平台
  • 网站版面布局结构seo搜索引擎优化工具
  • 网站的透明图片怎么做杭州网站优化公司哪家好
  • 聊城做网站的b站推广入口2023mmm
  • 哪些网站可以免费做推广上海免费关键词排名优化
  • 推广网站推荐黄冈便宜的网站推广怎么做
  • 外包做网站哪家好免费单页网站在线制作
  • 武汉做网站的培训机构百度投诉中心人工电话号码
  • 潍坊网站建设招聘百度商城
  • 阿里云怎么做淘客网站网络营销推广平台
  • 网站怎么建设dw如何免费推广自己的网站
  • 如何给网站死链接做404重庆人力资源和社会保障网官网
  • 网站建设域名费seo长尾关键词
  • 网站开发培训流程百度商家平台登录
  • 网站模板间距自己做网站建设
  • 甘南州城乡建设局网站无线网络优化工程师
  • 有域名自己做网站吗网店怎么推广和宣传
  • 加强和改进网站建设建设方案新东方厨师学费价目表
  • 网站建设手续百度打开
  • 城阳网站建设优化什么建立生育支持政策体系
  • 德保网站建设杭州seo网站哪家好
  • wordpress高亮宁波seo推荐推广渠道
  • 深圳建筑工地招工seo服务外包报价