"denv" is a library that manages the deployment environment, which can be one of development, integration, staging, or production.
- You import the library: "github.com/nao1215/denv"
- You set the environment variable "APP_ENV" to one of "development", "integration", "staging", or "production".
- You create a new Env instance with denv.NewEnv().
func Example() {
if err := os.Setenv("APP_ENV", denv.Staging); err != nil {
fmt.Println("Error setting environment variable:", err)
return
}
// Create a new Env instance.
env, err := denv.NewEnv()
if err != nil {
fmt.Println("Error creating environment:", err)
return
}
// Print the current environment.
fmt.Printf("Current environment: %s\n", env.String())
// Check if it's the development environment.
if env.IsDevelopment() {
fmt.Println("This is the development environment.")
} else {
fmt.Println("This is not the development environment.")
}
// Check if it's the production environment.
if env.IsProduction() {
fmt.Println("This is the production environment.")
} else {
fmt.Println("This is not the production environment.")
}
// Output:
// Current environment: staging
// This is not the development environment.
// This is not the production environment.
}
- Linux
- Mac
- Windows
- FreeBSD
- OpenBSD
- NetBSD
- DragonflyBSD
- go version 1.20 or later