env

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Overview

Package env provides functions for loading and parsing configuration files in the '.env' format.

The '.env' format is a simple key-value format, with one key-value pair per line. Lines that begin with a '#' character are treated as comments and are ignored. Keys and values may be surrounded by quotes, but this is not required.

The LoadConfig function can be used to load a configuration file and parse its key-value pairs into a struct or map. Converter functions can be provided to specify how each key's value should be parsed.

The LoadEnv function can be used to load a configuration file and set the corresponding environment variables for the current process.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadConfig

func LoadConfig(filename string, config interface{}) error

The config struct is passed as an argument and the LoadConfig function uses reflection to examine the fields of the struct and compare the key name with the "name" tag. If a match is found, it uses a switch statement to check the field's Kind and parse the value accordingly. If the fields are not primitive types or the key is not found in the struct, it returns an error.

func LoadEnv

func LoadEnv(filename string) error

LoadEnv loads the key-value pairs from a configuration file in the '.env' format and sets the corresponding environment variables for the current process. If a key is already set in the environment, it is overwritten. Lines that begin with a '#' character are treated as comments and are ignored. Keys and values may be surrounded by quotes, but this is not required.

If the file does not exist or cannot be read, an error is returned.

Types

type KeyValuePair

type KeyValuePair struct {
	Key   string
	Value string
}

KeyValuePair represents a key-value pair in an '.env' file.

Jump to

Keyboard shortcuts

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