iniconf

package module
v0.0.0-...-03cddbc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2019 License: Apache-2.0 Imports: 5 Imported by: 5

README

iniconf

The official website

描述

使用iniconf更简单的读取go的ini配置文件以及根据特定格式的各种配置文件。

安装方法

go get github.com/clod-moon/goconf

使用方法

ini配置文件格式样列

[database]
username = root
password = password
hostname = localhost

[admin]
username = root
password = password

[nihao]
username = root
password = password

初始化

conf := goini.InitConfig("./conf/conf.ini") //iniconf.SetConfig(filepath) 其中filepath是你ini 配置文件的所在位置

获取单个配置信息

username := conf.GetValue("database", "username") //database是你的[section],username是你要获取值的key名称
fmt.Println(username) //root

删除一个配置信息

conf.DeleteValue("database", "username")	//username 是你删除的key
username = conf.GetValue("database", "username")
if len(username) == 0 {
	fmt.Println("username is not exists") //this stdout username is not exists
}

添加一个配置信息

conf.SetValue("database", "username", "chun")
username = conf.GetValue("database", "username")
fmt.Println(username) //chun 添加配置信息如果存在[section]则添加或者修改对应的值,如果不存在则添加section

获取所有配置信息

conf.GetAllSetion() //返回map[string]map[string]string的格式 即setion=>key->value

iniconf

About

使用iniconf更简单的读取go的ini配置文件以及根据特定格式的各种配置文件。

<<<<<<< HEAD

install

go get github.com/clod-moon/goconf

use example

conf.ini

[database]
username = root
password = password
hostname = localhost

[admin]
username = root
password = password

[nihao]
username = root
password = password

initialize

conf := goini.InitConfig("./conf/conf.ini") //goini.InitConfig(filepath) filepath = directory+file

To obtain a single configuration information

username := conf.GetValue("database", "username") //username is your key you want get the value
fmt.Println(username) //root

To delete a configuration information

conf.DeleteValue("database", "username")	//username is your delete the key
username = conf.GetValue("database", "username")
if len(username) == 0 {
	fmt.Println("username is not exists") //this stdout username is not exists
}

Add a configuration information

conf.SetValue("database", "username", "chun")
username = conf.GetValue("database", "username")
fmt.Println(username) //chun Adding/section configuration information if there is to add or modify the value of the corresponding, if there is no add section

Get all the configuration information

conf.GetAllSetion() //return map[string]map[string]string  example:setion=>key->value

example

func main() {

	conf := iniconf.InitConfig("./config.ini")

	for key,value :=range conf.Conflist {
		fmt.Println(key)
		for k,v := range value{
			fmt.Println(k,":",v)
		}
	}
	fmt.Println(conf.GetValue("esinfo","addr"))

	conf.SetValue("esinfo","addr","127.100.100.100")

	fmt.Println(conf.GetValue("esinfo","addr"))
}

output

esinfo
addr : 127.0.0.1
port : 9200
index : case
type : case
127.0.0.1
127.100.100.100

Process finished with exit code 0

Documentation

Index

Constants

View Source
const (
	RENAME = "rename"
	REMOVE = "remove"
	CHANGE = "change"
)

Variables

This section is empty.

Functions

func CheckErr

func CheckErr(err error) string

Types

type Config

type Config struct {
	Conflist map[string]map[string]string //configuration information slice
	// contains filtered or unexported fields
}

func InitConfig

func InitConfig(filepath string) *Config

Create an empty configuration file

func (*Config) DeleteValue

func (c *Config) DeleteValue(section, name string) bool

Delete the corresponding key values 删除相应的键值

func (*Config) DeleteValueToFile

func (c *Config) DeleteValueToFile(section, name string) bool

delete the corresponding key value and update the configuration file at the same time 删除相应的键值,同时更新配置文件 暂未实现,等待后续

func (*Config) GetAllSetion

func (c *Config) GetAllSetion() map[string]map[string]string

获取所有配置项 List all the configuration file

func (*Config) GetValue

func (c *Config) GetValue(section, name string) string

To obtain corresponding value of the key values

func (*Config) SetValue

func (c *Config) SetValue(section, key, value string)

Set the corresponding value of the key value, if not add, if there is a key change 设置键值的对应值,如果没有添加,如果有键更改

func (*Config) SetValueToFile

func (c *Config) SetValueToFile(section, key, value string)

Update the configuration file at the same time 更新内存的时候,同时更新配置文件 暂未实现,等待后续

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL