jsonconfig

package module
v0.0.0-...-64ccc7d Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2018 License: MIT Imports: 3 Imported by: 0

README

Go jsconfig

Kailash Nadh, January 2015

MIT License

What?

jsconfig is a tiny JSON configuration file parser for Go lang with support for comments. Really, JSON spec doesn't include comments, but a configuration file without helpful comments is a pain to deal with.

Moreover, JSON for configuration files is extremely powerful when combined with structs, enabling to effortlessly load complex, nested data structures with Go's JSON Unmarshaling.

Installation (go 1.1+)

go get github.com/knadh/jsonconfig

Usage

Sample file: config.json

Notice the comments

{
	// url to the site
	"url": "http://google.com",

	"methods": ["GET", "POST"], // supported methods

	"always_load": true,

	// nested structure with different types
	"module": {
		"name": "Welcome",
		"route": "/",
		"port": 8080
	}
}
Loading the configuration
package main

import (
	"github.com/knadh/jsonconfig"
)

func main() {
	// setup the structure
	config := struct {
		Url string `json:"url"`

		Methods []string `json:"methods"`

		AlwaysLoad  bool `json:"always_load"`

		Module struct{
			Name string `json:"name"`
			Route string `json:"route"`
			Port int `json:"port"`
		} `json:"module"`
	}{}

	// parse and load json config
	err := jsonconfig.Load("config.json", &config)

	if err == nil {
		println("The url is", config.Url)
		println("Supported methods are", config.Methods[0], config.Methods[1])
		
		println("The module is", config.Module.Name, "on route", config.Module.Route)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(filename string, config interface{}) error

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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