ini parser for golang

logo

INI Parser & Write Library

ini parser and write library for Golang,easy-use,fast

Build Status version Go Report Card Mentioned in Awesome Go

Features

  • Can be read by []byte
  • Can be read by file
  • Supports file monitoring and takes effect in real time without reloading
  • Unmarshal to Struct
  • Marshal to Json
  • Write ini to File

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

cfg: {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[email protected]

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

Watch File

file := "./test.ini"

idoc := ini.New().WatchFile(file)
v := idoc.Section("s2").Get("k1")
fmt.Println("v:", v1)

// modify k1=v1   ==> k1=v2
time.Sleep(10 * time.Second)

v = idoc.Section("s2").Get("k1")
fmt.Println("v:", v1)

Output

v: v1
v: v2

Print file with 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": "[email protected]",
    "k7": "~/.path.txt",
    "k8": "./34/34/uh.txt",
    "k9": "234@!@#$%^&*()324"
  }
}

Set Ini

doc := `
k =v
[section]
a=b
c=d
`
ini := New().Load([]byte(doc)).Section("section")
fmt.Println("--------------------------------")
ini.Dump()

fmt.Println("--------------------------------")
ini.Set("a", 11).Set("c", 12.3).Section("").Set("k", "SET")
ini.Dump()

v := ini.Section("section").GetInt("a")

if v != 11 {
    t.Errorf("Error: %d", v)
}

v1 := ini.GetFloat64("c")

if v1 != 12.3 {
    t.Errorf("Error: %f", v1)
}

v2 := ini.Section("").Get("k")
if v2 != "SET" {
    t.Errorf("Error: %s", v2)
}

Wirte Ini

filename := "./save.ini"
ini := New().Set("a1", 1)
ini.Save(filename)
fmt.Println(ini.Err())

ini2 := New().Set("a1", 1).Section("s1").Set("a2", "v2")
ini2.Save(filename)
fmt.Println(ini2.Err())

// ------

doc := `
; 123
c11=d12312312
# 434

[section]
k=v
; dsfads 
;123
#3452345


[section1]
k1=v1

[section3]
k3=v3
`
ini3 := New().Load([]byte(doc))
ini.Save("./save.ini")

file content

; 123
c11 = d12312312

# 434
[section]
k = v

; dsfads 
;123
#3452345
[section1]
k1 = v1

[section3]
k3 = v3

Dump AST struct

INIDocNode {
    CommentNode {
        Comment: ; this is comment
        Line: 0
    }
    CommentNode {
        Comment: ; author levene
        Line: 1
    }
    CommentNode {
        Comment: ; date 2021-8-1
        Line: 2
    }
    KVNode {
        Key: a
        Value: '23'34?::'<>,.'
        Line: 5
    }
    KVNode {
        Key: c
        Value: d
        Line: 6
    }
    Section {
        Section: [s1]
        Line: 8
        KVNode {
            Value: 67676
            Line: 9
            Key: k
        }
        KVNode {
            Key: k1
            Value: fdasf
            Line: 10
        }
        KVNode {
            Value: 4w2
            Line: 11
            Key: k2
        }
    }
    CommentNode {
        Comment: # comment
        Line: 13
    }
    CommentNode {
        Line: 14
        Comment: # 12.0.0.1
    }
    Section {
        Section: [s2]
        Line: 15
        KVNode {
            Value: 3
            Line: 17
            Key: k
        }
        KVNode {
            Value: 945
            Line: 20
            Key: k2
        }
        KVNode {
            Key: k3
            Value: -435
            Line: 21
        }
        KVNode {
            Line: 22
            Key: k4
            Value: 0.0.0.0
        }
        KVNode {
            Line: 24
            Key: k5
            Value: 127.0.0.1
        }
        KVNode {
            Key: k6
            Value: [email protected]
            Line: 25
        }
        KVNode {
            Key: k7
            Value: ~/.path.txt
            Line: 27
        }
        KVNode {
            Line: 28
            Key: k8
            Value: ./34/34/uh.txt
        }
        KVNode {
            Key: k9
            Value: 234@!@#$%^&*()324
            Line: 30
        }
        KVNode {
            Key: k10
            Value: '23'34?::'<>,.'
            Line: 31
        }
    }
}

License

MIT

Similar Resources

Go-config - Config parser for go that supports environment vars and multiple yaml files

go-multiconfig This package is able to parse yaml config files. It supports gett

Jun 23, 2022

✨Clean and minimalistic environment configuration reader for Golang

Clean Env Minimalistic configuration reader Overview This is a simple configuration reading tool. It just does the following: reads and parses configu

Jan 8, 2023

Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.

genv Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables

Dec 21, 2022

Golang library for managing configuration data from environment variables

envconfig import "github.com/kelseyhightower/envconfig" Documentation See godoc Usage Set some environment variables: export MYAPP_DEBUG=false export

Dec 26, 2022

A better way to marshal and unmarshal YAML in Golang

YAML marshaling and unmarshaling support for Go Introduction A wrapper around go-yaml designed to enable a better way of handling YAML when marshaling

Jan 4, 2023

Golang Configuration tool that support YAML, JSON, TOML, Shell Environment

Configor Golang Configuration tool that support YAML, JSON, TOML, Shell Environment (Supports Go 1.10+) Usage package main import ( "fmt" "github.c

Dec 29, 2022

🔥🔥 🌈 Golang configuration,use to Viper reading from remote Nacos config systems. Viper remote for Naocs.

Viper remote for Nacos Golang configuration,use to Viper reading from remote Nacos config systems. Viper remote for Naocs. runtime_viper := viper.New(

Dec 6, 2022

A simple means of configuring Open Telemetry instrumentation in Golang

Otel GO Starter A simple means of getting the Open Telemetry global instrumentation configure and started Get Started Using the Otel GO Starter allows

Dec 6, 2022

A lightweight config center written by golang.

A lightweight config center written by golang.

Jan 21, 2022
A simple INI file parser and builder.

ini A simple INI file parser and builder. What Read INI files, or build them section by section. Datatypes are kept very simple. The main fuctions are

Nov 20, 2021
A golang package for parsing ini-style configuration files

Mini Mini is a simple ini configuration file parser. The ini syntax supported includes: The standard name=value Comments on new lines starting with #

Jan 7, 2023
Tinyini - Bare-bones Go library for reading INI-like configuration files

tinyini tinyini is a minimalistic library for parsing INI-like configuration files. example configuration file globalkey = globalvalue [section] key

Jan 10, 2022
INI Loader written in Go

go-ini INI Loader written in Go Single threaded & simple Examples Read all params func (app MyApp) onParam(name string, value string) bool { app.c

Feb 11, 2022
TOML parser for Golang with reflection.

THIS PROJECT IS UNMAINTAINED The last commit to this repo before writing this message occurred over two years ago. While it was never my intention to

Jan 6, 2023
TOML parser and encoder library for Golang

TOML parser and encoder library for Golang TOML parser and encoder library for Golang. This library is compatible with TOML version v0.4.0. Installati

Oct 11, 2022
Nginx Configuration Golang Parser

Nginx Configuration Golang Parser

Oct 21, 2022
🔎🪲 Malleable C2 profiles parser and assembler written in golang

goMalleable ?? ?? Malleable C2 profiles parser and assembler written in golang Table of Contents Introduction Installation Usage Examples Introduction

Oct 24, 2022
parser for configuration files

config-loader config-loader supports to load config files and convert to map values. Supported format json Usage import ( "fmt" "github.com/tharun2

Nov 27, 2021