Skip to content

Instantly share code, notes, and snippets.

@craicoverflow
Last active February 28, 2019 21:21
Show Gist options
  • Save craicoverflow/deff89a1ba897c6a9a827578123913c3 to your computer and use it in GitHub Desktop.
Save craicoverflow/deff89a1ba897c6a9a827578123913c3 to your computer and use it in GitHub Desktop.
Using the config struct in Go
package main
import (
"fmt"
"log"
"github.com/joho/godotenv"
"github.com/craicoverflow/go-environment-variables-example/config"
)
// init is invoked before main()
func init() {
// loads values from .env into the system
if err := godotenv.Load(); err != nil {
log.Print("No .env file found")
}
}
func main() {
conf := config.New()
// Print out environment variables
fmt.Println(conf.GitHub.Username)
fmt.Println(conf.GitHub.APIKey)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment