-
It seems that Viper currently only supports reading key-value content from package main
import "github.com/spf13/viper"
func main() {
viper.AddConfigPath(".")
viper.SetConfigFile(".env")
if err := viper.ReadInConfig(); err != nil {
panic("can't read config")
}
// handle key value
// ...
} However, when I try to read the package main
import "github.com/spf13/viper"
func main() {
viper.AddConfigPath(".")
// change here
viper.SetConfigType(".local")
viper.SetConfigName(".env")
// end
if err := viper.ReadInConfig(); err != nil {
panic("can't read config")
}
// handle key value
// ...
} So there is any way to let Viper read those files? |
Beta Was this translation helpful? Give feedback.
Answered by
keelus
Oct 4, 2023
Replies: 1 comment 1 reply
-
It supports it. You have to specify the type first, and then the file name: func main() {
viper.SetConfigType("env")
viper.SetConfigFile(".env.local")
if err := viper.ReadInConfig(); err != nil {
fmt.Println(".env.local not found. Trying with .env ...")
viper.SetConfigFile(".env")
if err2 := viper.ReadInConfig(); err2 != nil {
panic("Error reading the .env file")
} else {
fmt.Println(".env loaded")
}
} else {
fmt.Println(".env.local loaded")
}
// Handle keys
} |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
100gle
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It supports it. You have to specify the type first, and then the file name: