ini

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2023 License: MIT, MIT Imports: 8 Imported by: 1

README

功能

a simple ini file parser
一个简单的ini文件的解析器

ini文件(ini file)


[global]

pid = run/php-fpm.pid
error_log = /data1/logs/php-fpm_error.log
log_level = notice
emergency_restart_threshold = 10
emergency_restart_interval = 1m
process_control_timeout = 5s
daemonize = yes

简单的使用-simple use

package main

import (
	"fmt"
	"github.com/kjx98/golib/ini"
)

func main() {
	t_iniconfig()
}

func t_iniconfig() {
	cf, err := goini.ParserConfig("./conf/simple.conf", false)
	if err != nil {
		fmt.Println("get config faild")
		return
	}
	//获取配置
	fmt.Println(cf.GetConfig("global", "error_log",""))
	fmt.Println(cf.GetConfig("global", "log_level",""))
	fmt.Println(cf.GetConfig("global", "emergency_restart_threshold",""))
	fmt.Println(cf.GetConfig("global", "emergency_restart_interval",""))
	fmt.Println(cf.GetConfig("global", "process_control_timeout",""))
	fmt.Println(cf.GetConfig("global", "daemonize",""))

	//将配置写入到新的文件
	goini.SaveConfigToFile(cf, "./conf/simple_bak.conf")

	cf, err = goini.ParserConfig("./conf/simple_bak.conf", false)
	if err != nil {
		fmt.Println("get config faild")
		return
	}

	fmt.Println(cf.GetConfig("global", "error_log",""))
	fmt.Println(cf.GetConfig("global", "log_level",""))
	fmt.Println(cf.GetConfig("global", "emergency_restart_threshold",""))
	fmt.Println(cf.GetConfig("global", "emergency_restart_interval",""))
	fmt.Println(cf.GetConfig("global", "process_control_timeout",""))
	fmt.Println(cf.GetConfig("global", "daemonize",""))

}

代码输出 output

/data1/log/php-fpm_error.log
notice
10
1m
5s
yes
/data1/log/php-fpm_error.log
notice
10
1m
5s
yes

单元测试/性能测试 unittest/benchmarktest

BenchmarkParserConfig-4           	  100000	     12054 ns/op
BenchmarkParserConfig2-4          	  100000	     11961 ns/op
BenchmarkParserConfigParallel-4   	  200000	      7165 ns/op
PASS
ok  	config	4.174s

Documentation

Index

Constants

View Source
const (
	DEFAULT_NAME   = "default_key"
	SectionComment = "comment-section-%s"
)

Variables

View Source
var (
	FORMAT_ERROR = errors.New("ini format error")
	NOT_FOUND    = errors.New("not found config")
)

Functions

func NewSection

func NewSection(k, v string) *sectionConfig

创建一个配置

func SaveConfigToFile

func SaveConfigToFile(cf *IniConfig, filename string) bool

将数据写入到文件, 无法保证配置的顺序

Types

type IniConfig

type IniConfig struct {
	Sections map[string]sectionConfigs
}

func NewIniConfig

func NewIniConfig() (*IniConfig, error)

创建一个空的config

func ParserConfig

func ParserConfig(filename string, reload bool) (*IniConfig, error)

解析config

func (*IniConfig) DelConfigData

func (cf *IniConfig) DelConfigData(secting, name string) bool

删除配置

func (*IniConfig) GetConfig

func (cf *IniConfig) GetConfig(secting, name string, defV string) string

获取配置, defV 为默认

func (*IniConfig) GetConfigDouble

func (cf *IniConfig) GetConfigDouble(secting, name string, defV float64) float64

获取配置的各种方式 double, defV 为默认

func (*IniConfig) GetConfigInt

func (cf *IniConfig) GetConfigInt(secting, name string, defV int) int

获取配置的各种方式 int, defV 为默认

func (*IniConfig) PutConfig

func (cf *IniConfig) PutConfig(secting, name, val string) bool

更新或者创建配置

Jump to

Keyboard shortcuts

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