Gonfigure - Read and write config files in go

Gonfigure

Go Reference

Reads ini files in golang.

Reading INI Files

Load file

  • File can be loaded from gonfigure.LoadINI() function.

  • It reads the given ini file and returns a INIobject which can be later used to extract sections and parameter values.

  • Consider the following code for simply loading the ini file

objINI, err := gonfigure.LoadINI("/pathToFile.ini")
if err != nil {
	fmt.Println(err)
	panic(err)
}

Sections

  • Array of sections can be returned by calling the gonfigure.GetSections() function.

  • It requires

    • the INIobject as argument.
  • Returns array of section names and error.

    • nil error is returned in case of success
  • Consider the following example.

	fmt.Println(gonfigure.GetSections(dat))

Parameters

  • Array of parameter names for a given section can be returned by calling the gonfigure.GetParametersFromSection() function.

  • It requires

    • the INIobject as argument.

    • section Name

  • Returns array of parameters names and error.

    • nil error is returned in case of success
  • Consider the following example.

	parameters, err := gonfigure.GetParametersFromSection(dat, "Developer")
	if err != nil {
		panic(err)
	}

Parameter Values

  • Parameter Value of parameter names for a given section can be returned by calling the gonfigure.GetParametersFromSection() function.

  • It requires

    • the INIobject as argument.

    • section Name

    • parameter Name

  • Returns parameter value and error.

    • nil error is returned in case of success
  • Consider the following example.

	val, err := gonfigure.GetParameterValue(dat, "Developer", "name")
	if err != nil {
		panic(err)
	}

Example code

Following does all the steps mentioned in the Reading INI Files section

package main

import (
	"fmt"

	"github.com/IbrahimShahzad/gonfigure"
)

func main() {
	objINI, err := gonfigure.LoadINI("../test_file/single_section.ini")

	if err != nil {
		fmt.Println(err)
		panic(err)
	}

	for _, section := range gonfigure.GetSections(objINI) {
		fmt.Printf("Section: %v\n", section)
		parameters, err := gonfigure.GetParametersFromSection(objINI, section)
		if err != nil {
			panic(err)
		}
		for _, param := range parameters {
			val, err := gonfigure.GetParameterValue(objINI, section, param)
			if err != nil {
				panic(err)
			}
			fmt.Printf("Param: %v Value: %v\n", param, val)
		}
		fmt.Printf("\n")
	}
}
  • The input file used is single_section.ini in test_file directory

  • The output of the program is as follows:

> go run main.go
Section: sectionA
Param: parameterName Value: parameterValue
Param: parameterName1 Value: parameterValue1
Param: parameterName2 Value: parameterValue2
Param: name Value: john

Section: sectionB
Param: parameterName3 Value: parameterValue1

Section: Developer
Param: age Value: 23
Param: name Value: ibrahim
Owner
Ibrahim Shahzad
Just Curious.
Ibrahim Shahzad
Similar Resources

Simple, useful and opinionated config loader.

aconfig Simple, useful and opinionated config loader. Rationale There are many solutions regarding configuration loading in Go. I was looking for a si

Dec 26, 2022

Composable, observable and performant config handling for Go for the distributed processing era

Konfig Composable, observable and performant config handling for Go. Written for larger distributed systems where you may have plenty of configuration

Dec 11, 2022

Sidecar to watch a config folder and reload a process when it changes

A small (3MB uncompressed docker image), efficient (via inotify) sidecar to trigger application reloads when configuration changes.

Dec 29, 2022

Viper wrapper with config inheritance and key generation

piper - Simple Wrapper For Viper Single Source of Truth Generated Key Structs, No Typo Config Inheritance Multiple Config Strategies Support Cache For

Sep 26, 2022

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

A lightweight yet powerful config package for Go projects

Config GoLobby Config is a lightweight yet powerful config package for Go projects. It takes advantage of env files and OS variables alongside config

Dec 11, 2022

go implementation of lightbend's HOCON configuration library https://github.com/lightbend/config

HOCON (Human-Optimized Config Object Notation) Configuration library for working with the Lightbend's HOCON format. HOCON is a human-friendly JSON sup

Dec 3, 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
⚙️ Dead Simple Config Management, load and persist config without having to think about where and how.

Configo Dead Simple Config Management, load and persist config without having to think about where and how. Install go get github.com/UltiRequiem/conf

Apr 6, 2022
Package ini provides INI file read and write functionality in Go.

INI Package ini provides INI file read and write functionality in Go. Features Load from multiple data sources(file, []byte, io.Reader and io.ReadClos

Dec 29, 2022
Quick and easy way to load config files based on a simple set of rules.
Quick and easy way to load config files based on a simple set of rules.

config Quick and easy way to load config files based on a simple set of rules. Project inspired by https://github.com/lorenwest/node-config Important

Apr 9, 2021
Jul 4, 2022
Read files into environment variables and execute command

read-file-to-env -- Read files into environment variables and execute command Example use: read-file-to-env -one-line=HOST=/etc/hostname sh -c 'echo h

Nov 12, 2021
Config files for my GitHub profile.

hcltm Threat Modeling with HCL Overview There are many different ways in which a threat model can be documented. From a simple text file, to more in-d

Dec 7, 2021
Config files for my GitHub profile.
Config files for my GitHub profile.

swag ?? English ∙ 简体中文 Swag converts Go annotations to Swagger Documentation 2.0. We've created a variety of plugins for popular Go web frameworks. Th

Dec 7, 2021
nflex - common interface to parsed config files

nflex - common interface to parsed config files Install: go get github.com/muir/

Dec 29, 2021
Config files for my GitHub profile.
Config files for my GitHub profile.

Distributed server for social and realtime games and apps. Features Users - Register/login new users via social networks, email, or device ID. Storage

Jan 13, 2022
Quickly read variables from environment files

go-quick-env Quickly read variables from environment files The best way to import environment variables to your code, is by using .env files. This lib

May 11, 2021