GithubHelp home page GithubHelp logo

libconfig's Introduction

主要针对配置文件解析

go get github.com/sosop/libconfig

一、解析ini文件

app.ini

appname = sosop_app
mode = dev



[prod]
db = [email protected]:3306/dbname
port = 80

[dev]
db = mysql@localhost:3306/dbname
port = 8080

[test]
db = [email protected]:3306/dbname
port = 8088
package main

import (
	"fmt"
	"github.com/sosop/libconfig"
)

func main() {
	iniConfig := libconfig.NewIniConfig("app.ini")
	appname := iniConfig.GetString("appname")
	mode := iniConfig.GetString("mode")
	devDB := iniConfig.GetString("dev::db")
	testPort := iniConfig.GetInt("test::port")
	fmt.Println(appname, mode, devDB, testPort)
}

输出:sosop_app dev mysql@localhost:3306/dbname 8088

二、解析json

config.json

{
	"redisCluster": [
		{"host": "192.168.1.100", "port": 6379}, 
		{"host": "192.168.1.101", "port": 6380},
		{"host": "192.168.1.102", "port": 6381}],
	"dbCluster": [
		{"host": "172.20.10.8", "port": 3306}, 
		{"host": "172.20.10.9", "port": 3308},
		{"host": "172.20.10.10", "port": 3310}
	]
}
type StoreConfig struct {
	RedisCluster []struct {
		Host string `json:"host"`
		Port int    `json:"port"`
	} `json:"redisCluster`
	DbCluster []struct {
		Host string `json:"host"`
		Port int    `json:"port"`
	} `json:"dbCluster"`
}

func main() {
	storeConf := &StoreConfig{}
	libconfig.NewJsonConfig("config.json", storeConf)
	fmt.Println(*storeConf)
}

输出:{[{192.168.1.100 6379} {192.168.1.101 6380} {192.168.1.102 6381}] [{172.20.10.8 3306} {172.20.10.9 3308} {172.20.10.10 3310}]}

三、解析xml

config.xml

<?xml version="1.0"?>
<config>
	<appname>testXml</appname>
	<host>0.0.0.0</host>
  	<port>8888</port>
	
	<group>
		<value>log</value>
		<value>queue</value>
	</group>
	
	
	<es name="es1">
		<host>192.168.1.2</host>
		<port>7989</port>
		<shard>1</shard>
	</es>
	<es name="es2">
		<host>192.168.1.3</host>
		<port>7986</port>
		<shard>2</shard>
	</es>
	<es name="es3">
		<host>192.168.1.4</host>
		<port>7988</port>
		<shard>3</shard>
	</es>
</config>
type ES struct {
	Name  string `xml:"name,attr"`
	Host  string `xml:"host"`
	Port  int    `xml:"port"`
	Shard int    `xml:"shard"`
}

type XMLConfig struct {
	Appname string   `xml:"appname"`
	Host    string   `xml:"host"`
	Port    int      `xml:"port"`
	Group   []string `xml:"group>value"`
	ESS     []ES     `xml:"es"`
}

func main() {
	xmlConf := &XMLConfig{}
	libconfig.NewXmlConfig("config.xml", xmlConf)
	fmt.Println(*xmlConf)
}

输出:{testXml 0.0.0.0 8888 [log queue] [{es1 192.168.1.2 7989 1} {es2 192.168.1.3 7986 2} {es3 192.168.1.4 7988 3}]}

四、yaml

go-yaml

libconfig's People

Watchers

sosop-h avatar

Forkers

chixsh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.