Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created June 16, 2020 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/db966096ee90545c11472da67f639774 to your computer and use it in GitHub Desktop.
Save podhmo/db966096ee90545c11472da67f639774 to your computer and use it in GitHub Desktop.
GITHUB_USERNAME=craicoverflow
GITHUB_API_KEY=TCtQrZizM1xeo1v92lsVfLOHDsF7TfT5lMvwSno
MAX_USERS=10
USER_ROLES=admin,super_admin,guest
DEBUG_MODE=false
package main
import (
"fmt"
"log"
"github.com/caarlos0/env/v6"
"github.com/joho/godotenv"
)
type GitHubConfig struct {
Username string `env:"GITHUB_USERNAME"`
APIKey string `env:"GITHUB_API_KEY"`
}
type Config struct {
GitHub GitHubConfig
DebugMode bool `env:"DEBUG_MODE"`
UserRoles []string `env:"USER_ROLES"`
MaxUsers int `env:"MAX_USERS"`
}
func main() {
// loads values from .env into the system
if err := godotenv.Load(); err != nil {
log.Print("No .env file found")
}
var conf Config
if err := env.Parse(&conf); err != nil {
panic(err)
}
// Print out environment variables
fmt.Println(conf.GitHub.Username)
fmt.Println(conf.GitHub.APIKey)
fmt.Println(conf.DebugMode)
fmt.Println(conf.MaxUsers)
// Print out each role
for _, role := range conf.UserRoles {
fmt.Println(" ", role)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment