Documentation
¶
Overview ¶
Package envfile contains functions to (un)marshal EnvironmentFile to/from Go types.
This package is using the same 'style' as the the standard library encoding/json package for (un)marshaling EnvironmentFile (dot env) syntax.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Marshal ¶
Marshal returns the EnvironmentFile encoding of v.
The "omitempty" option specifies that the field should be omitted from the encoding if the field has an empty value.
Examples of struct field tags:
// Field appears in EnvironmentFile as variable "MY_NAME". Field string `env:"MY_NAME"` // Field appears in EnvironmentFile as variable "FIELD". Field string` // Field appears in EnvironmentFile as variable "MYNAME" and // the field is omitted from the object if its value is empty. Field string `env:"MYNAME,omitempty"` // Field appears in EnvironmentFile as variable "FIELD" (the default), but // the field is skipped if empty. // Note the leading comma. Field int `env:",omitempty"`
Only string fields are supported and it will return a ErrorUnsupportedType when fields with other types are not explicitly ignored.
Example ¶
values := struct {
Name string
MySetting string `env:"MY_SETTING"`
Empty string `env:",omitempty"`
}{
Name: "foo",
MySetting: "https://127.0.0.1",
}
out, err := Marshal(values)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(string(out))
Output: NAME=foo MY_SETTING=https://127.0.0.1
func Unmarshal ¶
Unmarshal parses the EnvironmentFile encoded data and stores the result in the value pointed to by v.
Example ¶
data := []byte(`FOO=bar
DB=test
# Comments and empty lines are ignored
EMPTY=
IGNORED=valuenotread
`)
v := struct {
Foo string
Database string `env:"DB"`
Empty string `env:",omitempty"`
Ignored string `env:"-"`
}{}
err := Unmarshal(data, &v)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println("Foo=" + v.Foo)
fmt.Println("Database=" + v.Database)
fmt.Println("Empty=" + v.Empty)
fmt.Println("Ignored=" + v.Ignored)
Output: Foo=bar Database=test Empty= Ignored=
Types ¶
type ErrorLineParsing ¶
type ErrorLineParsing struct {
LineNumber int
}
ErrorLineParsing is returned when a env line can not be parsed.
func (ErrorLineParsing) Error ¶
func (e ErrorLineParsing) Error() string
Error implements the error interface.
type ErrorUnsupportedType ¶
ErrorUnsupportedType is returned when the value is or contains unsupported types.
func (ErrorUnsupportedType) Error ¶
func (e ErrorUnsupportedType) Error() string
Error implements the error interface.