首页 > 其他分享 >golang viper 处理TOML 特殊的arrar和array of table

golang viper 处理TOML 特殊的arrar和array of table

时间:2023-03-14 18:33:40浏览次数:49  
标签:string relay lights ip golang arrar TOML viper port

参考:https://github.com/spf13/viper/issues/213
知识点:go类型断言
toml config:

[src_isntances]
# i=[{ip="dasds",port="asdas"},{ip="dffdafs",port="afasdsdas"}]
i1=[{ip="localhost",port="1435",user="",password=""},{ ip="locdlhost",port="1435",user="",password=""}]
i2=[{ip="localhost",port="1437",user="",password=""},{ ip="l7st",port="1435",user="",password=""}]

[[lights]]
relay = 1
name = "Night"
[[lights]]
relay = 2
name = "Day"
[[lights]]
relay = 3
name = "Low Intensity"
[[lights]]
relay = 8
name = "Blackout"

go code:

	type inst struct {
		Ip       string
		Port     string
		User     string
		Password string
	}
	var i map[string][]inst
	//instances

	viper.UnmarshalKey("src_isntances", &i)
	for _, v := range i {
		fmt.Println(v)
	}

	presets, ok := viper.Get("lights").([]interface{})
	if !ok {
		fmt.Println("incorrectly configured light presents")
	} else {
		for _, table := range presets {
			if m, ok := table.(map[string]interface{}); ok { // type assert here
				fmt.Println(cast.ToInt(m["relay"])) // need cast, "github.com/spf13/cast"
				// cast 'name' too
			}
		}
	}

标签:string,relay,lights,ip,golang,arrar,TOML,viper,port
From: https://www.cnblogs.com/ls11736/p/17215912.html

相关文章

  • Golang Block 到底是什么? `i:=i` 合法? 为什么能解决闭包变量冲突?
    GolangBlock到底是什么?i:=i合法?为什么能解决闭包变量冲突?什么?你告诉我i:=i不仅合法,而且还常用。甚至能解决并发编程中的变量冲突?以下这段代码出自golang官方的......
  • (转)golang goquery selector(选择器) 示例大全
    原文:https://juejin.cn/post/6844903552867893255最近研究Go爬虫相关的知识,使用到goquery这个库比较多,尤其是对爬取到的HTML进行选择和查找匹配的内容时,goquery的选择器......
  • golang示例项目 客户信息关系系统
    1.需求分析1)模拟实现基于文本界面的《客户信息管理软件》2)该软件能够实现对客户对象的插入、修改和删除(用切片实现),并能够打印客户明细表2.项目界面设计1)主菜单页面---......
  • 【转】Golang Reflect反射的使用详解1 --- makeFunc的使用
     转,原文: https://studygolang.com/articles/12300 ---------------------------------- Whatyouarewastingtodayistomorrowforthosewhodiedyesterday......
  • 04 Golang 运算符
    一、算术运算符运算符描述+相加-相减*相乘/相除%求余代码示例:1packagemain2​3import"fmt"4​5funcmain(){6//......
  • Golang的defer用法
    直观理解deferpackagemainimport("fmt")funcmain(){fmt.Println("deferbegin")//将defer放入延迟调用栈deferfmt.Println(1)def......
  • Golang使用命令行改变PATH路径
    goenv-wENV_VAR=value这是内置在goCLI中的跨平台解决方案,将来应该可以为您节省一些时间。例:goenv-wGOPATH=/your/desired/path输入goenv以检查当前环境......
  • Golang项目使用Dockerfile部署
    前言关于在构建golang编写的web项目中使用dockerfile的一些总结。通过查阅资料后,写下了如下配置:一般模式会安装golang编译环境,镜像文件包会比较大。#构建golang运行......
  • (转)golang 读写文件的四种方式
    原文:https://blog.csdn.net/whatday/article/details/103938124读文件读取的文件放在file/test:也就是file包下的test这个文件,里面写多一点文件读文件方式一:利用ioutil.R......
  • golang 自行实现一个base64加密
    packagemainimport( "fmt" "strconv")constbase64table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"funcMybase64(astring){......