-
-
Save podhmo/db966096ee90545c11472da67f639774 to your computer and use it in GitHub Desktop.
This file contains 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
GITHUB_USERNAME=craicoverflow | |
GITHUB_API_KEY=TCtQrZizM1xeo1v92lsVfLOHDsF7TfT5lMvwSno | |
MAX_USERS=10 | |
USER_ROLES=admin,super_admin,guest | |
DEBUG_MODE=false |
This file contains 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 ( | |
"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