loadenv

package module
v0.0.0-...-c9c73f4 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2024 License: MIT Imports: 6 Imported by: 1

README

loadenv

Loadenv provides two options to load from environment files.

Unmarshal

Unmarshal loads environment variables from the filepaths specified (defaults to .env) as values in a struct.

Usage:
package main

import (
  "github.com/tiredkangaroo/loadenv"
)

type EnvironmentVariables struct {
  USERNAME string // defaults to required, will error out if not provided
  SSLMODE  bool `required:"false"` // not required because it is specified in the struct tag
}

func main() {
  var environment EnvironmentVariables
  err := loadenv.Unmarshal(&environment, ...filepaths string)
  // handle err here
  fmt.Println("username:", environment.USERNAME)
}
Errors:
  • reading a file fails
  • parsing a line in the file that has bad syntax
  • a required variable is missing from any of the files
  • the value provided for the key cannot be assigned into the type of the structfield

Load

Load loads environment variables from the filepaths specified (defaults to .env).

Usage:
package main

import (
  "os"
  "github.com/tiredkangaroo/loadenv"
)

func main() {
  err := loadenv.Load(...filepaths string)
  // handle err here
  os.Getenv("POSTGRES_CONNECTION_URI")
}
Errors:
  • reading a file fails
  • parsing a line in the file that has bad syntax
  • the setenv() syscall fails

Expected Environment File Syntax

  • All spaces in a line will be trimmed.
  • All values are a strings (no quotation marks unless you want them in the string).
Example
USERNAME=user1
SSLMODE=false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load

func Load(filepaths ...string) error

Load loads environment variables from the filepaths specified. If no filepaths are specified, it defaults to reading .env. It may return an error when a file cannot be read, the file has bad syntax, or the syscall to set the environment variable fails.

func Unmarshal

func Unmarshal(s any, filepaths ...string) error

Unmarshal unmarshals environment variables into a struct. If no filepaths are specified, it defaults to reading .env.

Supported types are: string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, and uint64.

A struct field can specify whether or not the environment variable is required to be provided in any of the .env files with the `required` struct tag. If it is not provided, it defaults to true.

It may return an error if s is not a pointer to a struct, a file cannot be read, the file has bad syntax, a required field is not provided, or a provided field cannot be made into the type in the struct field.

Types

This section is empty.

Jump to

Keyboard shortcuts

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