golang 开源项目: 配置解析模块--config
在golang中,配置文件经常使用json格式。json格式的语法,有些繁琐,尤其是出现嵌套的时候,每一块都需要大括号包裹,看起来很臃肿。
本着简单易用的原则,个人开发了一个配置解析模块config,已在github开源。
下面介绍配置解析模块config的语法和使用方法。
基本语法
基本字符定义如下:
# 注释
= key=value, 赋值
[] 区域,可以表示一个结构体
[[]] 区域的数组,表示结构体的数组
使用tab进行缩进,每缩进一个tab,就表示嵌套一级。
配置文件示例:
#comment like this
host = example.com
ipaddr = 192.168.1.56
port = 43
compression = on
max_conn = 68182
port_enable = true
order = 98, 652, 31, 599, 566, 12, 208
[monitor]
enabled = true
ip = 192.168.1.161
[MAC]
mac1 = AA:BB:CC
mac2 = DD:EE:FF
port = 3698
cluster = 127.0.0.1, 192.168.16.163
[portal]
enabled =true
ip = 192.168.8.198
port = 3036
#array
[[cluster]]
addr = 10.0.1.160
wgh = 20
[[cluster]]
addr = 10.12.201.187
wgh = 10
废话不多说,直接看例子。
安装
go get github.com/yangeagle/config
例子
例子配置文件simple.conf:
#comment like this
host = example.com
ipaddr = 192.168.1.56
port = 43
compression = on
#comment like this
height = 8848.16, 693.254, 1.230, 996
# google
active = false
#array
cluster = 192.168.8.171, 192.168.8.170, 192.168.8.156
distance = 1896
temprature = 90.88
top_level = 9123456
max_conn = 68182
order = 98, 652, 31, 599, 566, 12, 208
示例代码:
package main
import (
"fmt"
"github.com/yangeagle/config"
)
type ConfigOption struct {
Hostname string `config:"host"`
Addr string `config:"ipaddr"`
PortNum int `config:"port"`
Height []float32 `config:"height"`
Active bool `config:"active"`
Clusters []string `config:"cluster"`
Dist int `config:"distance"`
Temp float64 `config:"temprature"`
TopLevel *int `config:"top_level"`
NumConn int `config:"max_conn"`
Order []int `config:"order"`
}
const configFile = "simple.conf"
func main() {
confParser := config.NewConfig()
err := confParser.ParseFile(configFile)
if err != nil {
fmt.Println("ParseFile failed:", err)
return
}
confOption := new(ConfigOption)
err = confParser.Unmarshal(confOption)
if err != nil {
fmt.Println("Unmarshal failed:", err)
return
}
fmt.Println("Hostname:", confOption.Hostname)
fmt.Println("Addr:", confOption.Addr)
fmt.Println("Port:", confOption.PortNum)
fmt.Println("Height:", confOption.Height)
fmt.Println("Active:", confOption.Active)
fmt.Println("Clusters:", confOption.Clusters)
fmt.Println("Dist:", confOption.Dist)
fmt.Println("Temp:", confOption.Temp)
fmt.Println("TopLevel:", *confOption.TopLevel)
fmt.Println("NumConn:", confOption.NumConn)
fmt.Println("Order:", confOption.Order)
}
以上是配置解析模块代码库config使用方法,如果想了解更多,请访问项目地址:https://github.com/yangeagle/config
欢迎使用,如果你觉得不错,欢迎加星:)
golang 开源项目: 配置解析模块--config的更多相关文章
- Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目
Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...
- .NET大型B2C开源项目nopcommerce解析——项目结构
.NET大型B2C开源项目nopcommerce解析——项目结构 编写本文档是为了向程序员说明nopcommerce的解决方案结构,亦是程序员开发nopcommerce的居家必备良书.首先nopcom ...
- golang开源项目qor快速搭建网站qor-example运行实践
最近想找几个基于Go语言开发的简单的开源项目学习下,分享给大家,github上有心人的收集的awesome-go项目集锦:github地址 发现一个Qor项目: Qor 是基于 Golang 开发的的 ...
- vue-cli的webpack模版项目配置解析
上一篇文章已经分析了build/dev-server.js,里面使用到了其他config文件. 那么我们这篇文章,按着dev-server.js的使用顺序,来分析下其他文件. 首选,调用check-v ...
- Android开源项目xUtils HttpUtils模块分析(转)
xUtils是github上的一个Android开源工具项目,其中HttpUtils模块是处理网络连接部分,刚好最近想整理下Android网络编程知识,今天学习下xUtils中HttpUtils. x ...
- Golang开源项目
谷歌官方维护了一个基于go语言的开源项目列表: https://github.com/golang/go/wiki/Projects 其中有非常多的优秀项目值得学习,有几百行代码适合新手阅读的项目,也 ...
- Android 开源项目android-open-project解析之(二) GridView,ImageView,ProgressBar,TextView
五.GridView StaggeredGridView 同意非对齐行的GridView,类似Pinterest的瀑布流.而且跟ListView一样自带View缓存,继承自ViewGroup 项目地址 ...
- vue-cli的webpack模版项目配置解析-build/dev-server.js
我们在使用vue-cli搭建vuejs项目(Vuejs实例-01使用vue-cli脚手架搭建Vue.js项目)的时候,会自动生成一系列文件,其中就包含webpack配置文件.我们现在来看下,这些配置到 ...
- Go语言(golang)开源项目大全
转http://www.open-open.com/lib/view/open1396063913278.html内容目录Astronomy构建工具缓存云计算命令行选项解析器命令行工具压缩配置文件解析 ...
随机推荐
- Delphi PopupMenu组件
- SQL SERVER 查询被锁的表、解锁表
-- 查询被锁的表select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName f ...
- MySQL更新字段来自另一个表的count()值
假设有文章post和评论comment两个表,文章表记录有评论的数量,但是这个值我们要一次更新. 如下,现在post表的comment_count都是0,我们的目标是:执行一个SQL语句,让其把统计c ...
- linux 能ping通IP但无法解析域名
vi /etc/nsswitch.conf hosts: files dns networks: files 改成: hosts: files dns wins networks: files 最近碰 ...
- k8s-insight测试
eureka apiVersion: v1 kind: Pod metadata: name: eureka labels: ccb: eureka spec: containers: - name: ...
- H5+JAVA的文件上传,断点续传
这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...
- java实现大文件上传
文件上传是最古老的互联网操作之一,20多年来几乎没有怎么变化,还是操作麻烦.缺乏交互.用户体验差. 一.前端代码 英国程序员Remy Sharp总结了这些新的接口 ,本文在他的基础之上,讨论在前端采用 ...
- HDU 3669 [Cross the Wall] DP斜率优化
问题分析 首先,如果一个人的\(w\)和\(h\)均小于另一个人,那么这个人显然可以被省略.如果我们将剩下的人按\(w[i]\)递增排序,那么\(h[i]\)就是递减. 之后我们考虑DP. 我们设\( ...
- BZOJ 4386 Luogu P3597 [POI2015]Wycieczki (矩阵乘法)
题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=4386 (luogu) https://www.luogu.org/pro ...
- vue中的js绑定样式
添加class 对象形式添加 activated为true时p标签的class为activated false时为空 <div id="app"> <p :c ...