envconfig

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2021 License: MIT Imports: 6 Imported by: 6

Documentation

Overview

Package envconfig provides a convenient way to load config from environment variables into a struct using https://github.com/kelseyhightower/envconfig.

Example
package main

import (
	"time"

	"github.com/pthethanh/micro/config/envconfig"
)

func main() {
	var conf struct {
		Name        string            `envconfig:"NAME" default:"micro"`
		Address     string            `envconfig:"ADDRESS" default:"0.0.0.0:8000"`
		Secret      string            `envconfig:"SECRET"`
		Fields      []string          `envconfig:"FIELDS" default:"field1,field2"`
		ReadTimeout time.Duration     `envconfig:"READ_TIMEOUT" default:"30s"`
		Enable      bool              `envconfig:"ENABLE" default:"true"`
		Map         map[string]string `envconfig:"MAP" default:"key:value,key1:value1"`
	}
	envconfig.Read(&conf)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Close

func Close() error

Close close the default config reader.

func Read

func Read(ptr interface{}, opts ...config.ReadOption) error

Read read configuration from environment to the target ptr.

Example (WithOptions)
package main

import (
	"github.com/pthethanh/micro/config"
	"github.com/pthethanh/micro/config/envconfig"
)

func main() {
	var conf struct {
		Name    string `envconfig:"NAME" default:"micro"`
		Address string `envconfig:"ADDRESS" default:"0.0.0.0:8000"`
		Secret  string `envconfig:"SECRET"`
	}
	envconfig.Read(&conf, config.WithPrefix("HTTP"))
}
Output:

Types

type Config

type Config struct {
}

Config implements config.Reader.

func (*Config) Close

func (env *Config) Close() error

Close implements config.Reader interface.

func (*Config) Read

func (env *Config) Read(ptr interface{}, opts ...config.ReadOption) error

Read implements config.Reader interface.

Jump to

Keyboard shortcuts

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