please i need help in reading from an env.json file. Anytime i run my code i always get this error
Error reading config file, Config File "env" Not Found in "[/Users/franklynomonade/go/src/bitbucket.org/core_backend/cmd/server/bitbucket.org/core_backend/pkg/app/config]"
"/Users/franklynomonade/go/src/bitbucket.org/core_backend/cmd/server"
is the path where my main.go file is in, while "bitbucket.org/core_backend/pkg/app/config"
is the path where the env.json file is located.
I believe the path where the env.json file should be read is "bitbucket.org/core_backend/pkg/app/config"
and not "/Users/franklynomonade/go/src/bitbucket.org/core_backend/cmd/server/bitbucket.org/core_backend/pkg/app/config"
I am using github.com/spf13/viper
package to read the env.json file.
func LoadConfig() {
viper.AddConfigPath(myPath)
viper.SetConfigName("env")
viper.SetConfigType("json")
// searches for config file in given paths and read it
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("error reading config file: %s", err)
}
// confirm which config file is used
log.Printf("Using s: %s\n", viper.ConfigFileUsed()
}
Any idea on how i can fix this?
CodePudding user response:
You can use relative or absolute paths, usually, it's better to use relative paths since you don't need to specify the path outside of your project.
When using the relative path, it searches the path relative to the folder in which you executed the binary.
Assuming this is the tree:
├── core_backend
│ ├── cmd
│ └── pkg
│ ├── api
│ └── app
│ └── config
│ └── env.json
Let's say you run your program in the core_backend
directory, you would set your variable myPath
to this value
"pkg/app/config"