Skip to content

Instantly share code, notes, and snippets.

@craicoverflow
Last active March 2, 2019 19:49
Show Gist options
  • Save craicoverflow/ea6d06a2d819c706029e888f1a6a7d28 to your computer and use it in GitHub Desktop.
Save craicoverflow/ea6d06a2d819c706029e888f1a6a7d28 to your computer and use it in GitHub Desktop.
Load environment variables from a .env file in Go using godotenv
package main
import (
"log"
"github.com/joho/godotenv"
"fmt"
"os"
)
// 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() {
// Get the GITHUB_USERNAME environment variable
githubUsername, exists := os.LookupEnv("GITHUB_USERNAME")
if exists {
fmt.Println(githubUsername)
}
// Get the GITHUB_API_KEY environment variable
githubAPIKey, exists := os.LookupEnv("GITHUB_API_KEY")
if exists {
fmt.Println(githubAPIKey)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment