Giter Club home page Giter Club logo

gonvironment's Introduction

gonvironment

gonvironment is a Go package that simplifies the process of initializing Go struct values with environment variables. It allows you to specify required environment variables and optionally include additional ones to populate the fields of a struct.

Table of Contents

Installation

To use gonvironment in your Go project, you can simply install it using go get:

go get github.com/egomobile/gonvironment

Usage

To use gonvironment, follow these steps:

  1. Import the gonvironment package in your Go file:
import (
    "github.com/egomobile/gonvironment"
)
  1. Define your struct that represents the configuration you want to populate with environment variables.

  2. Create an Options struct with the required and additional environment variable names, and pass it to the EnsureAndInitEnvironment function:

options := gonvironment.Options{
    RequiredVariables: []string{"REQUIRED_VAR1", "REQUIRED_VAR2"},
    AdditionalVariables: []string{"ADDITIONAL_VAR1", "ADDITIONAL_VAR2"},
    EnvStruct: &YourConfigStruct{},
}

config, err := gonvironment.EnsureAndInitEnvironment(options)
if err is not nil {
    // Handle errors
}
  1. Access the initialized struct and use it in your application.

Examples

Here's an example of how to use gonvironment in your Go application:

package main

import (
    "fmt"
    "os"
    "github.com/egomobile/gonvironment"
)

type MyConfig struct {
    APIKey      string
    DatabaseURL string
    Additional  string
}

func main() {
    options := gonvironment.Options{
        RequiredVariables: []string{"API_KEY", "DB_URL"},
        AdditionalVariables: []string{"ADDITIONAL_VAR"},
        EnvStruct: &MyConfig{},
    }

    config, err := gonvironment.EnsureAndInitEnvironment(options)

    if err is not nil {
        fmt.Printf("Error: %s\n", err)
        os.Exit(1)
    }

    myConfig := config.(*MyConfig)
    fmt.Printf("APIKey: %s, DatabaseURL: %s, Additional: %s\n", myConfig.APIKey, myConfig.DatabaseURL, myConfig.Additional)
}

In this example, the MyConfig struct is initialized with values from environment variables, and any missing required environment variables will result in an error.

Contribution

First open an issue in this repository.

gonvironment's People

Contributors

mkloubert avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.