Categorygithub.com/tiredkangaroo/loadenv
repositorypackage
0.0.0-20240831160258-c9c73f452bdd
Repository: https://github.com/tiredkangaroo/loadenv.git
Documentation: pkg.go.dev

# 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