zoukankan      html  css  js  c++  java
  • 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

    欢迎使用,如果你觉得不错,欢迎加星:)

  • 相关阅读:
    【NET CORE微服务一条龙应用】第一章 网关使用与配置
    111
    test
    再来一个测试
    测试博客
    flutter 中的json解析
    关于flutter -app开发过程中的问题及解决方式总结
    使用Mybatis-plus通过自定义Sql查询只有主键为null的问题
    Centos 6中keepalived作为服务启动
    CentOS6 开放、关闭防火墙相关端口命令
  • 原文地址:https://www.cnblogs.com/lanyangsh/p/11221547.html
Copyright © 2011-2022 走看看