en Home Smtp Sometimes I fall in love with code jgdb A Gopher named Gophy Encode/Decode Golang data to Gob files GoWatch File Access and Create Time Go Snippets Ping Citadel vCard Sync Your Everyday Go Tools Cases Conv Go jkl Encode/Decode between struct and byte slice Crypt HDKeys Wordlist HDKeys Recover

Cfg

Cfg is a json configuration package for Go

Features:

Docs:

import "bitbucket.org/gotamer/cfg"

func Load(filename string, o interface{}) (err error)

Load gets your config from the json file, and fills your struct with the option

func Save(filename string, o interface{}) (err error)	

Save will save your struct to the given filename, this is a good way to create a json template

Example:

    package main

    import (
        "fmt"
        "bitbucket.org/gotamer/cfg"
        "os"
    )

    var Cfg *MainCfg

    type MainCfg struct {
        Name  string
    }

    func main() {
        Cfg = &MainCfg{"defaultAlias"}
        cfgpath := os.Getenv("GOPATH") + "/etc/myappname.json"
        err := cfg.Load(cfgpath, Cfg)
        if err != nil {
            cfg.Save(cfgpath, Cfg)
            fmt.Println("\n\tPlease edit your configuration at: ", cfgpath, "\n")
            os.Exit(0)
        }
        fmt.Printf("%s", Cfg)
    }

Links

Tags: code go


First Created: 2015-01-28T19:54:59Z
Last Modified: 2026-04-05 19:35