structenv

package module
v0.0.0-...-707c683 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2023 License: MIT Imports: 10 Imported by: 0

README

structenv

Set structs from environment variables in Golang.

Example

package main

import (
	"fmt"
	"log"

	"github.com/jotadrilo/structenv"
)
type Person struct {
	Name   string  `env:"NAME"`
	Age    int     `env:"AGE"`
	Height float64 `env:"HEIGHT"`
	Weight float64 `env:"WEIGHT"`
}

func main() {
    p := &Person{Name: "Bob", Age: 45, Height: 1.75, Weight: 76.8}
	fmt.Printf("#1 %s (%.1fkg, %.1fm) is %d years old\n", p.Name, p.Weight, p.Height, p.Age)

	if err := structenv.Parse(p); err != nil {
		log.Fatal(err)
	}
	fmt.Printf("#2 %s (%.1fkg, %.1fm) is %d years old\n", p.Name, p.Weight, p.Height, p.Age)
}
$ go run ./person.go
#1 Bob (76.800000kg, 1.750000m) is 45 years old
#2 Bob (76.800000kg, 1.750000m) is 45 years old

$ NAME=Alice AGE=45 HEIGHT=1.67 WEIGHT=62 go run ./person.go
#1 Bob (76.8kg, 1.8m) is 45 years old
#2 Alice (62.0kg, 1.7m) is 45 years old

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(i interface{}) error

Parse parses the environment variables to assign values in the provided interface.

func ParseEnv

func ParseEnv(env []string, i interface{}) error

ParseEnv parses the provided environment variables list in the form key=value to assign values in the provided interface.

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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