8000 GitHub - wlevene/ini at v0.1.1
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

wlevene/ini

Repository files navigation

INI Parser Library

ini parser library for Golang, easy-use, fast, use ast parse content

Features

  • 支持[]byte读取
  • 支持文件加载
  • 支持文件监控,实时生效,无需重新加载
  • 解构体解析
  • ini转json

Installation

go get github.com/wlevene/ini

Example

import (
	"fmt"

	"github.com/wlevene/ini"
)

GetValue

doc := `
[section]
k=v

[section1]
k1=v1
k2=1
k3=3.5
k4=0.0.0.0
`
	v1 := ini.New().Load([]byte(doc)).Section("section1").Get("k1")
	fmt.Println(v1)

Output

v1
	i := ini.New().Load([]byte(doc))
	v1 := i.Section("section1").Get("k1")
	v2 := i.GetInt("k2")
	v3 := i.GetFloat64("k3")
	v4 := i.Get("k4")
	v5 := i.GetIntDef("keyint", 10)
	v6 := i.GetDef("keys", "defualt")

	fmt.Printf("v1:%v v2:%v v3:%v v4:%v v5:%v v6:%v\n", v1, v2, v3, v4, v5, v6)

Output

v1:v1 v2:1 v3:3.5 v4:0.0.0.0 v5:10 v6:defualt

Marshal2Json

fmt.Println(string(i.Marshal2Json()))

Output

{"section":{"k":"v"},"section1":{"k1":"v1","k2":"1","k3":"3.5","k4":"0.0.0.0"}}

Unmarshal Struct

type TestConfig struct {
	K    string  `ini:"k" json:"k,omitempty"`
	K1   int     `ini:"k1" json:"k1,omitempty"`
	K2   float64 `ini:"k2"`
	K3   int64   `ini:"k3"`
	User User    `ini:"user"`
}

type User struct {
	Name string `ini:"name"`
	Age  int    `ini:"age"`
}
doc := `
k=v
k1=2
k2=2.2
k3=3

[user]
name=tom
age=-23
`

	cfg := TestConfig{}

	ini.Unmarshal([]byte(doc), &cfg)
	fmt.Println("cfg:", cfg)

Output

{v 2 2.2 3 {tom -23}}

Parse File

ini file

; this is comment
; author levene 
; date 2021-8-1


a='23'34?::'<>,.'
c=d

[s1]
k=67676
k1 =fdasf 
k2= sdafj3490&@)34 34w2

# comment 
# 12.0.0.1
[s2]

k=3


k2=945
k3=-435
k4=0.0.0.0

k5=127.0.0.1
k6=levene@github.com

k7=~/.path.txt
k8=./34/34/uh.txt

k9=234@!@#$%^&*()324
k10='23'34?::'<>,.'
file := "./test.ini"
ini.New().LoadFile(file).Section("s2").Get("k2")

fmt.Println(string(ini.Marshal2Json()))

Output

945

Print file json

file := "./test.ini"
fmt.Println(string(ini.New().LoadFile(file).Marshal2Json()))

Output

{
  "a": "'23'34?::'<>,.'",
  "c": "d",
  "s1": {
    "k": "67676",
    "k2": "34w2"
  },
  "s2": {
    "k": "3",
    "k10": "'23'34?::'<>,.'",
    "k2": "945",
    "k3": "-435",
    "k4": "0.0.0.0",
    "k5": "127.0.0.1",
    "k6": "levene@github.com",
    "k7": "~/.path.txt",
    "k8": "./34/34/uh.txt",
    "k9": "234@!@#$%^&*()324"
  }
}

TODO

  • wirte

About

ini parser for golang

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
0