env

package module
v0.0.0-...-da1bd2d Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2023 License: Apache-2.0 Imports: 5 Imported by: 2

README

env

Example

package main

import (
	"fmt"
	"github.com/eatmoreapple/env"
	"log"
	"os"
)

func main() {
	if err := os.Setenv("hello", "world"); err != nil {
		log.Fatal(err)
	}
	if err := os.Setenv("bool", "true"); err != nil {
		log.Fatal(err)
	}
	fmt.Println(env.Name("hello").String()) // world

	fmt.Println(env.Name("bool").Bool()) // true

	fmt.Println(env.Name("undefined").StringOrElse("undefined")) // undefined

	var entity struct {
		Hello string `env:"hello"`
		Bool  bool   `env:"bool"`
	}
	if err := env.Decode(&entity); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%+v", entity) // {Hello:world Bool:true}
	
	
	var defaultEntity struct{
		Value string `env:"NOT_FOUND" default:"default"`
    }
	if err := env.Decode(&defaultEntity); err != nil {
        log.Fatal(err)
    }
	
	fmt.Printf("%+v", defaultEntity) // {Value:default}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(v interface{}) error

Decode decodes environment variables into the given value.

func Encode

func Encode(v interface{}) error

Encode encodes the struct v into the environment

Types

type Env

type Env string

Env is a wrapper for os.Getenv

func Name

func Name(key string) Env

Name returns the name of the environment variable

func (Env) Bool

func (e Env) Bool() bool

Bool returns the value of the environment variable as a bool

func (Env) BoolOrElse

func (e Env) BoolOrElse(v bool) bool

BoolOrElse returns the value of the environment variable as a bool or the default value

func (Env) Float32

func (e Env) Float32() float32

Float32 returns the value of the environment variable as a float32

func (Env) Float32OrElse

func (e Env) Float32OrElse(v float32) float32

Float32OrElse returns the value of the environment variable as a float32 or the default value

func (Env) Float64

func (e Env) Float64() float64

Float64 returns the value of the environment variable as a float64

func (Env) Float64OrElse

func (e Env) Float64OrElse(v float64) float64

Float64OrElse returns the value of the environment variable as a float64 or the default value

func (Env) Int

func (e Env) Int() int

Int returns the value of the environment variable as an int

func (Env) Int16

func (e Env) Int16() int16

Int16 returns the value of the environment variable as an int16

func (Env) Int16OrElse

func (e Env) Int16OrElse(v int16) int16

Int16OrElse returns the value of the environment variable as an int16 or the default value

func (Env) Int32

func (e Env) Int32() int32

Int32 returns the value of the environment variable as an int32

func (Env) Int32OrElse

func (e Env) Int32OrElse(v int32) int32

Int32OrElse returns the value of the environment variable as an int32 or the default value

func (Env) Int64

func (e Env) Int64() int64

Int64 returns the value of the environment variable as an int64

func (Env) Int64OrElse

func (e Env) Int64OrElse(v int64) int64

Int64OrElse returns the value of the environment variable as an int64 or the default value

func (Env) Int8

func (e Env) Int8() int8

Int8 returns the value of the environment variable as an int8

func (Env) Int8OrElse

func (e Env) Int8OrElse(v int8) int8

Int8OrElse returns the value of the environment variable as an int8 or the default value

func (Env) IntOrElse

func (e Env) IntOrElse(v int) int

IntOrElse returns the value of the environment variable as an int or the default value

func (Env) IsEmpty

func (e Env) IsEmpty() bool

IsEmpty returns true if the environment variable is empty

func (Env) MustBool

func (e Env) MustBool() bool

MustBool returns the value of the environment variable as a bool or panics

func (Env) MustFloat32

func (e Env) MustFloat32() float32

MustFloat32 returns the value of the environment variable as a float32 or panics

func (Env) MustFloat64

func (e Env) MustFloat64() float64

MustFloat64 returns the value of the environment variable as a float64 or panics

func (Env) MustInt

func (e Env) MustInt() int

MustInt returns the value of the environment variable as an int or panics

func (Env) MustInt16

func (e Env) MustInt16() int16

MustInt16 returns the value of the environment variable as an int16 or panics

func (Env) MustInt32

func (e Env) MustInt32() int32

MustInt32 returns the value of the environment variable as an int32 or panics

func (Env) MustInt64

func (e Env) MustInt64() int64

MustInt64 returns the value of the environment variable as an int64 or panics

func (Env) MustInt8

func (e Env) MustInt8() int8

MustInt8 returns the value of the environment variable as an int8 or panics

func (Env) MustUint

func (e Env) MustUint() uint

MustUint returns the value of the environment variable as an uint or panics

func (Env) MustUint16

func (e Env) MustUint16() uint16

MustUint16 returns the value of the environment variable as an uint16 or panics

func (Env) MustUint32

func (e Env) MustUint32() uint32

MustUint32 returns the value of the environment variable as an uint32 or panics

func (Env) MustUint64

func (e Env) MustUint64() uint64

MustUint64 returns the value of the environment variable as an uint64 or panics

func (Env) MustUint8

func (e Env) MustUint8() uint8

MustUint8 returns the value of the environment variable as an uint8 or panics

func (Env) String

func (e Env) String() string

String returns the value of the environment variable

func (Env) StringOrElse

func (e Env) StringOrElse(v string) string

StringOrElse returns the value of the environment variable or the default value

func (Env) Uint

func (e Env) Uint() uint

Uint returns the value of the environment variable as an uint

func (Env) Uint16

func (e Env) Uint16() uint16

Uint16 returns the value of the environment variable as an uint16

func (Env) Uint16OrElse

func (e Env) Uint16OrElse(v uint16) uint16

Uint16OrElse returns the value of the environment variable as an uint16 or the default value

func (Env) Uint32

func (e Env) Uint32() uint32

Uint32 returns the value of the environment variable as an uint32

func (Env) Uint32OrElse

func (e Env) Uint32OrElse(v uint32) uint32

Uint32OrElse returns the value of the environment variable as an uint32 or the default value

func (Env) Uint64

func (e Env) Uint64() uint64

Uint64 returns the value of the environment variable as an uint64

func (Env) Uint64OrElse

func (e Env) Uint64OrElse(v uint64) uint64

Uint64OrElse returns the value of the environment variable as an uint64 or the default value

func (Env) Uint8

func (e Env) Uint8() uint8

Uint8 returns the value of the environment variable as an uint8

func (Env) Uint8OrElse

func (e Env) Uint8OrElse(v uint8) uint8

Uint8OrElse returns the value of the environment variable as an uint8 or the default value

func (Env) UintOrElse

func (e Env) UintOrElse(v uint) uint

UintOrElse returns the value of the environment variable as an uint or the default value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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