Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrNotSlice = errors.New("should be a slice") ErrNotSettable = errors.New("should be settable") ErrPrefixRequired = errors.New("prefix is required") )
View Source
var ( ErrNotPointer = errors.New("should be a pointer") ErrNotStruct = errors.New("should be a structure") )
View Source
var Setenv = os.Setenv
View Source
var Unsetenv = os.Unsetenv
Functions ¶
func Init ¶
Init reads and init configuration to `config` variable, which must be a reference of struct
Example ¶
type Server struct {
Addr string `json:"addr" envconfig:"SERVER_ADDR" default:"localhost:8080"`
}
type Postgres struct {
Host string `json:"host" envconfig:"POSTGRES_HOST" default:"localhost"`
Port string `json:"port" envconfig:"POSTGRES_PORT" default:"5432"`
User string `json:"user" envconfig:"POSTGRES_USER" default:"postgres"`
Password string `json:"password" envconfig:"POSTGRES_PASSWORD" default:"12345"`
}
type Redis struct {
Addrs []string `json:"addrs" envconfig:"REDIS_ADDR" default:"localhost:6379"`
}
type NATS struct {
ServerURL string `json:"server_url" envconfig:"NATS_SERVER_URL" default:"nats://localhost:4222"`
MaxReconnectionAttempts int `json:"max_reconnection_attempts" envconfig:"NATS_MAX_RECONNECT_ATTEMPTS" default:"5"`
ReconnectInterval config.Duration `json:"reconnect_interval" envconfig:"NATS_RECONNECT_INTERVAL" default:"2s"`
}
type Websocket struct {
Port string `json:"port" envconfig:"WEBSOCKET_PORT" default:"9876"`
}
var cfg struct {
Version string `envconfig:"VERSION" default:"0"`
Server Server `json:"server"`
Postgres Postgres `json:"postgres"`
Replicas []Postgres `json:"replicas" envprefix:"REPLICAS"`
Redis Redis `json:"redis"`
NATS NATS `json:"nats"`
Websocket Websocket `json:"websocket"`
}
if err := config.Init(&cfg, "testdata/config.json"); err != nil {
log.Fatalln(err)
}
fmt.Println(cfg)
Output: {0.0.1 {localhost:8080} {localhost 5432 postgres 12345} [{localhost 5433 replica0 12345} {localhost 5433 replica1 12345}] {[127.0.0.1:6377 127.0.0.1:6378 127.0.0.1:6379]} {nats://localhost:4222 5 2000000000} {9876}}
Types ¶
type Duration ¶
Duration provides marshal/unmarshal of duration as a string
func (Duration) MarshalJSON ¶
MarshalJSON Duration as a string
func (*Duration) UnmarshalJSON ¶
UnmarshalJSON Duration from float64/string
Click to show internal directories.
Click to hide internal directories.