andrey.shedko
andrey.shedko

Reputation: 3238

Go cannot find file specified

I'm trying to make simple read settings from config file. Both files - config.json and Settings.go, are in the same folder. But I'm always getting "The system cannot find the file specified." What I'm doing wrong?

func GetDbConnectionString() string {
    file, err := os.Open("config.json")
    if err != nil {
        log.Fatal(err)
    }
    decoder := json.NewDecoder(file)
    settings := Settings{}
    err1 := decoder.Decode(&settings)
    if err1 != nil {
        fmt.Println("error:", err1)
    }
    log.Print(&settings)
    return fmt.Sprintf("%s:%s@/%s", settings.login, settings.password, settings.database)
}

enter image description here

Upvotes: 1

Views: 12617

Answers (1)

Marc
Marc

Reputation: 21145

Your settings.json is not in the same directory as your main.go. If you invoke either go run main.go, or go build . && ./app, the current path will be .../app/ which does not contain the settings.json file.

Try copying your settings.json file to the same directory as your app, local invocation will work (it will still fail if you run from a separate directory though).

Upvotes: 8

Related Questions