confparser

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 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:
    • 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.

Installation

go get [github.com/](https://github.com/)<YOUR_USERNAME>/<YOUR_REPO_NAME>
Usage
1. Prepare a Configuration File
Create a config file (e.g., config.conf) that you want to parse.

Plaintext
# Example of config.conf
port 8080
host localhost

# Lines with 3 or more elements will be ignored
invalid_key too many values
2. Use in Your Go Code
You can parse directly from a file or a string, as long as it satisfies the io.Reader interface.

go
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 := <YOUR_PACKAGE_NAME>.Parse(f)
	if err != nil {
		log.Fatalf("Parse error: %v", err)
	}

	// 3. Retrieve values

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

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

func Parse

func Parse(r io.Reader) (*Config, 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]

Jump to

Keyboard shortcuts

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