gini

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2021 License: Apache-2.0 Imports: 12 Imported by: 4

README

gini

一个可以读写ini配置文件的小项目

项目地址:

https://github.com/gkzy/gini

特点

  • 支持reload
func (ini *INI) ReLoad() error

常用方法

  • Get
v := ini.Get("app_name")
  • SectionGet
v = ini.SectionGet("file", "include")
  • GetKeys
keys := ini.GetKeys("")
  • GetSections
sections := ini.GetSections()

demo

app.conf

conf/app.conf

app_name = Admin-Client
run_mode = dev
http_addr = 18090
auto_render = true
session_on = false
template_left = "<<"
template_right = ">>"

[user]
user = "root"
password = "123456"
host = "192.168.0.197"
port = 3306
dev = true

[redis]
host = "192.168.0.197"
port = 6379
db = 0
password = "123456"
maxidle = 50
maxactive = 10000

ini_test.go

package gini

import (
	"fmt"
	"log"
	"testing"
)

var (
	content = `
default = 1
abc = 2
[data]
host = 192.168.0.1
`
)

func Test1(t *testing.T) {

	// ini:=New("./conf")  指定目录
	ini := New()
	err := ini.Load("app.conf")
	if err != nil {
		log.Fatal(err)
	}

	// 读取default key
	v := ini.Get("app_name")
	fmt.Println(v)

	vb := ini.GetBool("session_on")
	fmt.Printf("bool : %#v \n", vb)

	vi, _ := ini.GetInt("http_addr")
	fmt.Printf("int : %#v \n", vi)

	// 读取指定section的key
	v = ini.SectionGet("file", "include")
	fmt.Printf("value = %s \n", v)

	//读取所有的section
	sections := ini.GetSections()
	fmt.Printf("sections:  %v \n", sections)

	//读取指定 section的所有key
	keys := ini.GetKeys("")
	for _, item := range keys {
		fmt.Println(item.K, item.V)
	}

	//读取include文件的配置
	keys = ini.GetKeys("samblog")
	for _, item := range keys {
		fmt.Println(item.K, item.V)
	}

	//写到一个新的文件
	_, err = ini.WriteFile("app_temp.conf", content)
	if err != nil {
		log.Fatal(err)
	}

}

Documentation

Index

Constants

View Source
const (
	//DefaultSection default section name
	DefaultSection = ""
	//DefaultLineSeparator default line sep
	DefaultLineSeparator = "\n"

	//DefaultKeyValueSeparator default k=v eeq
	DefaultKeyValueSeparator = "="
)

Variables

This section is empty.

Functions

This section is empty.

Types

type INI

type INI struct {
	// contains filtered or unexported fields
}

INI ini struct

func New

func New(path ...string) *INI

New return *INI

func (*INI) Get

func (ini *INI) Get(key string) (value string)

Get returns default section key

return string

func (*INI) GetBool

func (ini *INI) GetBool(key string) bool

GetBool get default section key

return bool

func (*INI) GetDirectory added in v0.0.2

func (ini *INI) GetDirectory() string

GetDirectory return config file directory

func (*INI) GetFileName added in v0.0.2

func (ini *INI) GetFileName() string

GetFileName GetFileName

func (*INI) GetFloat32

func (ini *INI) GetFloat32(key string) (float32, error)

GetFloat32 get default section key

return float32

func (*INI) GetFloat64

func (ini *INI) GetFloat64(key string) (float64, error)

GetFloat64 get default section key

return float64

func (*INI) GetInt

func (ini *INI) GetInt(key string) (int, error)

GetInt get default section key

return bool

func (*INI) GetInt64

func (ini *INI) GetInt64(key string) (int64, error)

GetInt64 get default section key

return int64

func (*INI) GetKeys

func (ini *INI) GetKeys(section string) KeySlice

GetKeys return all keys of the section

func (*INI) GetSections

func (ini *INI) GetSections() []string

GetSections return all sections of the file

return []string

func (*INI) Load

func (ini *INI) Load(filename string) error

Load load file from directory to ini data

func (*INI) LoadByte

func (ini *INI) LoadByte(data []byte, lineSep, kvSep string) error

LoadByte load byte to ini data

func (*INI) LoadReader

func (ini *INI) LoadReader(r io.Reader, lineSep, kvSep string) error

LoadReader load io reader to ini data

func (*INI) ReLoad added in v0.0.2

func (ini *INI) ReLoad() error

ReLoad reload file

func (*INI) SectionBool

func (ini *INI) SectionBool(section, key string) bool

SectionBool get bool value

func (*INI) SectionFloat32

func (ini *INI) SectionFloat32(section, key string) (float32, error)

SectionFloat32 get float32 value

func (*INI) SectionFloat64

func (ini *INI) SectionFloat64(section, key string) (float64, error)

SectionFloat64 returns float64

func (*INI) SectionGet

func (ini *INI) SectionGet(section, key string) (value string)

SectionGet return value

func (*INI) SectionInt

func (ini *INI) SectionInt(section, key string) (int, error)

SectionInt get section key value

return int

func (*INI) SectionInt64

func (ini *INI) SectionInt64(section, key string) (int64, error)

SectionInt64 returns int64

func (*INI) SectionSet added in v0.1.0

func (ini *INI) SectionSet(section, key string, value interface{})

SectionSet set section -> key ->value

func (*INI) Set added in v0.1.0

func (ini *INI) Set(key string, value interface{})

Set default section key value

func (*INI) SetDirectory added in v0.0.2

func (ini *INI) SetDirectory(dir string)

SetDirectory set config file directory

func (*INI) SetFileName added in v0.0.2

func (ini *INI) SetFileName(filename string)

SetFileName SetFileName

func (*INI) SetSectionMap added in v0.0.9

func (ini *INI) SetSectionMap(sectionMap SectionMap)

SetSectionMap set ini sectionMap

func (*INI) Write

func (ini *INI) Write(w io.Writer) error

Write to io.Writer

func (*INI) WriteFile added in v0.0.4

func (ini *INI) WriteFile(filename, content string) (n int, err error)

WriteFile write a new file

need filename and content

func (*INI) WriteOriginFile added in v0.0.5

func (ini *INI) WriteOriginFile() error

WriteOriginFile rewrite the origin file

type Key

type Key struct {
	K string `json:"k"`
	V string `json:"v"`
}

Key kv struct

type KeySlice

type KeySlice []*Key

KeySlice define keySlice

func (KeySlice) Len

func (m KeySlice) Len() int

Len KeySlice wort

func (KeySlice) Less

func (m KeySlice) Less(i, j int) bool

Less sort less imp

func (KeySlice) Swap

func (m KeySlice) Swap(i, j int)

Swap KeySlice wort

type SectionMap

type SectionMap map[string]KeySlice

SectionMap store map

Jump to

Keyboard shortcuts

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