Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

A golang package to ensure and initialize environment.

Notifications You must be signed in to change notification settings

egomobile/gonvironment

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

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.

About

A golang package to ensure and initialize environment.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%