confparser

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: MIT Imports: 4 Imported by: 0

README

config-file-parser-go

config-file-parser-go is a simple and lightweight Key-Value configuration file parser for Go. It easily parses simple, space-separated formats commonly found in Linux config files, making them easy to use within your Go applications.

Features

  • Simple: Zero external dependencies (uses only the Go standard library).
  • Lightweight: Accepts an io.Reader, ensuring a low memory footprint even when processing large files.
  • Intuitive Rules:
    • Use Parse() method.
    • Parses space- or tab-separated entries (e.g., key value).
    • Ignores anything after a # as a comment.
    • Safely skips empty lines and lines with three or more elements (e.g., key value1 value2) as invalid formats.
  • Multiple Values:
    • Use ParseMultipleValues() method
    • All values are stored into slices

Installation

go get github.com/ebishrimp/config-file-parser-go

Sample

Usage

  1. Prepare a Configuration File Create a config file (e.g., config.conf) that you want to parse.
# Example of config.conf
port 8080
host localhost

# Lines with 3 or more elements will be ignored
invalid_key too many values
# Example of config.conf which can be parsed by ParseMultipleValues(r io.Reader)
# The same key can hold multiple values
multiplevalues value1
multiplevalues value2

# Simultaneous description of values will be ignored
invalid_form value3 value4 value5
  1. Use in Your Go Code You can parse directly from a file or a string, as long as it satisfies the io.Reader interface.
package main

import (
	"fmt"
	"log"
	"os"

	confparser "github.com/ebishrimp/config-file-parser-go"
)

func main() {
	// 1. Open the file (you can also pass a string using strings.NewReader)
	f, err := os.Open("config.conf")
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	// 2. Parse the configuration
	conf, err := confparser.Parse(f)
	if err != nil {
		log.Fatalf("Parse error: %v", err)
	}

	// 3. Retrieve values

	// Get as a string
	host := conf.GetValue("host")
	fmt.Printf("Host: %s\n", host)

	// Get as an integer
	port, err := conf.IntGetValue("port")
	if err != nil {
		log.Printf("Failed to get port: %v", err)
	} else {
		fmt.Printf("Port: %d\n", port)
	}

	// Check if a key exists
	if conf.ExistsValue("invalid_key") {
		fmt.Println("invalid_key exists")
	} else {
		fmt.Println("invalid_key does not exist because it was skipped")
	}

	// 4. Parse multiple values
	multiConf, err := confparser.ParseMultipleValues(f)
	if err != nil {
		log.Fatalf("Parse error: %v", err)
	}

	// 5. Retrieve values

	// Get string slice
	multival := confparser.GetMultipleValues(multiConf, "multiplevalues")
	// multival[0] == "value1"
	// multival[1] == "value2"

	// Get a first contest of slice
	firstval := cnfparser.GetFirstValue(multiConf, "multiplevalues")
	// firstval == "value1"

	// Check if a key exists
	if conf.ExistsValue("invalid_form") {
		fmt.Println("invalid_form exists")
	} else {
		fmt.Println("invalid_form does not exist because it was skipped")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExistsMultipleValues added in v1.0.0

func ExistsMultipleValues(c *MultiConfig, key string) bool

return true if the key exists, otherwise return false

func GetFirstValue added in v1.0.0

func GetFirstValue(c *MultiConfig, key string) string

return the first value which is associated with the key, if the key does not exist, return empty string

func GetMultipleValues added in v1.0.0

func GetMultipleValues(c *MultiConfig, key string) []string

return a slice of strings which are associated with the key, if the key does not exist, return an empty slice

Types

type Config

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

func Parse

func Parse(r io.Reader) (*Config, error)

read and parse the configuration from the reader, return a Config struct, if there is an error during reading, return an error

func (*Config) ExistsValue

func (c *Config) ExistsValue(key string) bool

return true if the key exists, otherwise return false

func (*Config) GetValue

func (c *Config) GetValue(key string) string

return value which is associated with the key, if the key does not exist, return empty string

func (*Config) IntGetValue

func (c *Config) IntGetValue(key string) (int, error)

return value which is assocated with key and is converted to int, if the key does not exist or the value cannot be converted to int, return error

type MultiConfig added in v1.0.0

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

func ParseMultipleValues added in v1.0.0

func ParseMultipleValues(r io.Reader) (*MultiConfig, error)

read and parse the configuration from the reader, return a MultiConfig struct, if there is an error during reading, return an error. The MultiConfig struct should support multiple values for the same key, and the values should be stored in a slice of strings.

Jump to

Keyboard shortcuts

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