Last active
March 2, 2019 19:49
-
-
Save craicoverflow/ea6d06a2d819c706029e888f1a6a7d28 to your computer and use it in GitHub Desktop.
Load environment variables from a .env file in Go using godotenv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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