Git Product home page Git Product logo

simpleconfig's Introduction

Description

Simpleconfig is a library that gives your Go project a flexible configuration mechanism. It is inspired by the configuration system for Redis. It can read config from different sources (a file, string, or io.Reader), and write it to an arbitrary struct or map.

The simpleconfig format is primitive -- it ignores lines that are blank or start with a pound sign #. Otherwise, it expects lines to have the format key value, where key and value are strings separated by a blank space.

As an example of a configuration file, see redis.conf

Installation

Simpleconfig is a go package, so it can be installed with:

  • goinstall github.com/hoisie/simpleconfig
  • Clone the repo and run 'make install'
  • Or, just copy and paste simpleconfig.go into a new file in your project to avoid the dependency (but you won't get updates)

Usage

Simpleconfig has two methods:

func Read(source interface{}) (map[string]string, os.Error)
func Unmarshal(dst interface{}, source interface{}) os.Error

The first method takes a source (either a string, or an io.Reader), and returns a map[string]string holding the configuration. If the source argument is a string, it tries to open a file by the name, and if that fails, it treats the string itself as the configuration data.

The second method is more useful, it takes the configuration contained in source and tries to write it to the value represented by dst. For example, if you have a struct holding configuration variables, and a string with the values:

type Config struct{
    Option1 string
    Option2 bool
    Option3 int64
}

var configString = `
#Sample configuration

option1 hello
option2 true
option3 101
`

You can write:

var config Config
err := simpleconfig.Unmarshal(&config, configString)

simpleconfig's People

Contributors

hoisie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.