conf: github.com/mkmueller/conf Index | Examples | Files

package conf

import "github.com/mkmueller/conf"

Conf is a simple wrapper for json to help read and write configuration files.

Index

Examples

Package Files

conf.go

func Read Uses

func Read(file string, o interface{}) error

The Read function accepts a filename and a struct pointer. The contents the supplied json file will be unmarhslled into the supplied struct. An error value will be returned if one is encountered during json unmarshalling or during file reading. An error will be returned if a non-pointer is passed.

Code:


config_file := "myconfig.conf"

var config struct {
    Name string
    Addr string
    Port int
}

err := conf.Read(config_file, &config)
if err != nil {
    panic(err.Error())
}

func Write Uses

func Write(fl string, o interface{}) error

Write accepts a filename and a struct and writes the contents of the struct to a file in pretty json format. If the file exists, it will be overwitten. An error value will be returned if one is encountered during json marshalling or file writing.

Code:


config_file := "myconfig.conf"

config := struct {
    Name string
    Addr string
    Port int
}{
    "Server McServerface",
    "192.168.1.98",
    80,
}

err := conf.Write(config_file, config)
if err != nil {
    panic(err.Error())
}

Package conf imports 2 packages (graph). Updated 2017-11-20. Refresh now. Tools for package owners.