config

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2020 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(prefix string, configType Config) (interface{}, error)

New は prefix をもつ環境変数を configType にあわせて config を生成します。

Types

type Config

type Config interface{}

Config は

Example
type HogeConfig struct {
	Log struct {
		Out   string `default:"stdout"`
		Level string `default:"info"`
	}
	FugaService struct {
		Key string `required:"true"`
	} `envconfig:"FUGA_SERVICE"`
	ServiceDiscovery struct {
		Provider string `required:"true"`
		Endpoint string `required:"true"`
	} `envconfig:"SERVICE_DISCOVERY"`
}

os.Clearenv()
os.Setenv("HOGE_LOG_OUT", "stderr")
os.Setenv("HOGE_LOG_LEVEL", "debug")
os.Setenv("HOGE_FUGA_SERVICE_KEY", "fuga")
os.Setenv("HOGE_SERVICE_DISCOVERY_PROVIDER", "etcd")
os.Setenv("HOGE_SERVICE_DISCOVERY_ENDPOINT", "127.0.0.1:2379")

conf, err := New("HOGE", &HogeConfig{})
if err != nil {
	fmt.Printf("failed: %+v", err)
}

conf, ok := conf.(*HogeConfig)
if !ok {
	fmt.Printf("failed")
}

fmt.Printf("%+v", conf)
Output:

&{Log:{Out:stderr Level:debug} FugaService:{Key:fuga} ServiceDiscovery:{Provider:etcd Endpoint:127.0.0.1:2379}}

Jump to

Keyboard shortcuts

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