Help us shape the future of Go! Take the 2026 Go Developer Survey

gopts

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2019 License: MIT Imports: 6 Imported by: 1

README

Gopts 🔧

GoDoc Codecov License

Gopts is a simple library that loads environment variables into a given struct.

Installing

  1. Download and install it
$ go get -u github.com/victorgama/gopts
  1. Import it in your code:
import "github.com/victorgama/gopts"

Usage

This is a really short crash-course. More information can be found on godoc. Assuming an environment with two variables USERNAME and AUTO_RESTART set to Rômulo and false, respectively, running the following snippet of code:

package main

import (
    "github.com/victorgama/gopts"
    "fmt"
)

type Settings struct {
    Username    string
    AutoRestart bool    `default:"true"`
}

func main() {
    settings := gopts.LoadEnvs(Settings{}).(Settings)
    fmt.Println(settings.Username)
    fmt.Println(settings.AutoRestart)
}

Will yield the following output:

Rômulo
false

License

MIT License

Copyright (c) 2017-2019 Victor Gama

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Overview

Package gopts provide facilities to load environment variables into a struct.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadEnvs

func LoadEnvs(baseObj interface{}) interface{}

LoadEnvs is an alias to `LoadEnvsWithPrefix("", baseObj)`

func LoadEnvsWithPrefix

func LoadEnvsWithPrefix(prefix string, baseObj interface{}) interface{}

LoadEnvsWithPrefix loads data from environment variables into a managed provided by you. Useful for loading settings stored in the OS environment.

The following types are currently supported:

  • bool
  • int
  • int8
  • int16
  • int32
  • int64
  • []string
  • string

gopts will use fields of the provided object to match environment keys. For instance, it expects that a field named APIKey is available as `API_KEY` in the environment. Assuming the prefix argument is provided with a non-empty value "PREFLIGHT", the library will look for a env named PREFLIGHT_API_KEY.

Setting a gopts:"-" tag prevents gopts from filling the associated field.

A "default" tag may also be set. Its value will be set to the field in case it is absent from the environment vars set.

For instance, take the following struct:

type Settings struct {
    Username 		string
    SecretKey 		string	`default:"s3cr37"`
    AutoRestart 	bool	`default:"true"`
    IgnoredField 	string	`gopts:"-"`
}

and the following environment variables:

  • PREF_USERNAME=Rob
  • PREF_AUTO_RESTART=false

running the following snippet:

settings := gopts.LoadEnvsWithPrefix("pref", Settings{}).(Settings)

will yield the following result:

{
		Username: 		"Rob",
		SecretKey: 		"s3cr37",
		AutoRestart: 	false,
		IgnoredField:	""
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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