Skip to content
Switch branches/tags

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


Simple JSON config file.

To Use

import ""

//Loads the json file, creates it if one doesn't exist
cfg, err := LoadOrCreate("settings.json")
if err != nil {
	panic(fmt.Sprintf("Cannot load settings.json: %v", err)

//Return the value of url, if not found use the default ""
cfg.String("url", "")

cfg.Set("url", "")
err := cfg.Write()
if err != nil {
	panic("Cannot write settings.json: %v", err)

You can pass a slice filenames into the Load and LoadOrCreate, and it will use the first one it finds. If none are found when passed into LoadOrCreate, then the first file in the slice will be created.

You can also use the StandardFileLocations which will return a slice of standard config file locations for your OS. Generally follows this priority list:

  1. User locations are used before...
  2. System locations which are used before ...
  3. The immediate running directory of the application

The result set will be joined with the passed in filepath. Passing in a filepath with a leading directory is encouraged to keep your config folders organized.

For example a filepath of myApp/config.json might return the following on linux:


Note that parent folder paths (myApp in this example) are stripped for the last eligble file location so the config file will exist in the same directory as the running executable.

Below is a complete example of an application which would support configuration being supplied either via config files in standard OS locations, or Environment variables.

package main

import (


func main() {

	cfg, err := config.Load(config.StandardFileLocations("myApp/config.json")...)

	if os.IsNotExist(err) {
		// On Linux, no config files found in:
		//	/home/user/.config/myApp/config.json
		//	/etc/xdg/myApp/config.json
		//	/etc/myApp/config.json
		//	./config.json
		cfg = config.LoadEnv("MYAPP_")

	address := cfg.String("address", "")
	//should print on a fresh machine
	//export MYAPP_address= then run again to print



Simple JSON config file with standardized file locations and option ENV variable support



No releases published


No packages published