Error reading config file. Golang using viper package


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/]"

"/Users/franklynomonade/go/src/" is the path where my main.go file is in, while "" is the path where the env.json file is located.
I believe the path where the env.json file should be read is "" and not "/Users/franklynomonade/go/src/"

I am using package to read the env.json file.

here is the code that reads from the env.json file

func LoadConfig() {



    // 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?


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


Answered By – oren

Answer Checked By – Gilberto Lyons (GoLangFix Admin)

Leave a Reply

Your email address will not be published.