Skip to content

deepch/config

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 

Repository files navigation

JSON config parser and encoder for Go

Installation:

go get github.com/deepch/config

Examples

This package works JSON.

For the simplest example, consider some JSON file as just a list of keys and values:

{
  "Age": 25,
  "Cats": [
    "Cauchy",
    "Plato"
  ],
  "Pi": 3.14,
  "Perfection": [
    6,
    28,
    496,
    8128
  ],
  "DOB": "1987-07-05T05:45:00.000Z"
}

Which could be defined in Go as:

type Config struct {
  Age int
  Cats []string
  Pi float64
  Perfection []int
  DOB time.Time // requires `import time`
}

And then decoded with:

var conf Config
if _, err := config.Decode(jsonFileString, &conf); err != nil {
  // handle error
}

And then encode with:

if _, err := config.Encode(jsonFileString, &conf); err != nil {
  // handle error
}

You can also use struct tags if your struct field name doesn't map to a JSON key value directly:

some_key_NAME = "wat"
type json struct {
  ObscureKey string `json:"some_key_NAME"`
}

You can use omitempty

type json struct {
  ObscureKey string `json:"some_key_NAME,omitempty"`
}

If use omitempty and Encode not save zero 0 (defoult) data

You can line and char error

Decode Config Syntax Error File config/file.conf 
Line 5 Char 1 Error invalid character '1' looking for beginning of object key string

Releases

No releases published

Packages

No packages published

Languages