goenv
goenv copied to clipboard
Project-specific environment variables in Go.
GoEnv: Project-Specific Environment Variables in Go
An Introduction
"The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard."
Getting Started
Import
import "github.com/tmilewski/goenv"
Usage
goenv.Load()
Examples
Examples may be found in the /examples directory.
.env
HELLO="WORLD"
SENSITIVE_KEY="sensitive value"
YOUR_HOME=$HOME
main.go
import "fmt"
import "os"
import "github.com/tmilewski/goenv"
func init() {
err := goenv.Load()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func main() {
fmt.Println("HELLO", os.Getenv("HELLO")) // Prints "HELLO WORLD"
fmt.Println(os.Getenv("SENSITIVE_KEY")) // Prints "sensitive value"
fmt.Println(os.Getenv("HOME_DIR")) // Prints <your home directory>
}
Supported Go Versions
GoEnv is tested under 1.2, 1.3, 1.4.
Testing
go test -v
What's Next
- Substituting commands:
CWD=$(pwd)
- Make regular expressions more readable.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
License
GoEnv is released under the MIT License.
Thanks
Heavily influenced by the amazing work of Brandon Keepers on Ruby's dotenv.