nconfig

package module
v0.0.0-...-23f8519 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2020 License: MIT Imports: 4 Imported by: 0

README

go-nconfig

Build Status

Like a npm module of node-config a go package.

Quick Start

Install in your app directory. And edit the default config file.

go mod init github.com/you/hello
go get github.com/shinshin86/go-nconfig
mkdir config
vim config/default.json

Setup default.json

{
  "hostname": "localhost:8080",
  "database": {
    "protocol": "tcp(127.0.0.1:3306)",
    "dbname": "dbname-dev"
  }
}

Create main.go.

vim main.go
package main

import (
	"fmt"

	"github.com/shinshin86/go-nconfig"
)

func main() {
	config := nconfig.New("default")

	fmt.Println("hostname : ", config.Get("hostname"))
	fmt.Println("database.protocol : ", config.Get("database.protocol"))
	fmt.Println("database.dbname : ", config.Get("database.dbname"))
}
go run main.go
# ==> hostname :  localhost:8080
# ==> database.protocol :  tcp(127.0.0.1:3306)
# ==> database.dbname :  dbname-dev

When specify config file

Reading specify config file. ( Not set value is refer to default.json )

default.json

{
  "hostname": "localhost:8080",
  "database": {
    "protocol": "tcp(127.0.0.1:3306)",
    "dbname": "dbname-dev"
  },
  "dep1": {
    "val": "dev1",
    "dep2": {
      "val": "dev2",
      "dep3": {
        "val": "dev3",
        "dep4": {
          "val": "dev4",
          "dep5": {
            "val": "dev5",
            "dep6": {
              "val": "dev6"
            }
          }
        }
      }
    }
  }
}

production.json

{
  "hostname": "example.com",
  "database": {
    "dbname": "dbname-prod"
  },
  "dep1": {
    "val": "prod1",
    "dep2": {
      "val": "prod2",
      "dep3": {
        "val": "prod3",
        "dep4": {
          "val": "prod4",
          "dep5": {
            "val": "prod5",
            "dep6": {
              "val": "prod6"
            }
          }
        }
      }
    }
  }
}

main.go

package main

import (
	"fmt"

	"github.com/shinshin86/go-nconfig"
)

func main() {
	config := nconfig.New("production")

	fmt.Println("hostname : ", config.Get("hostname"))
	fmt.Println("database.protocol : ", config.Get("database.protocol"))
	fmt.Println("database.dbname : ", config.Get("database.dbname"))
	fmt.Println("dep6 value : ", config.Get("dep1.dep2.dep3.dep4.dep5.dep6.val"))
}
go run main.go
# ==> hostname :  example.com
# ==> database.protocol :  tcp(127.0.0.1:3306)
# ==> database.dbname :  dbname-prod
# ==> dep6 value :  prod6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	Name string
	// contains filtered or unexported fields
}

Config store on config name and config values

func New

func New(filename string) *Config

New is returns the config loaded with the specified configuration name

func (*Config) Get

func (config *Config) Get(key string) string

Get the value of the configuration file with specified key

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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