简体   繁体   English

Go找不到指定的文件

[英]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. config.json和Settings.go这两个文件都位于同一文件夹中。 But I'm always getting "The system cannot find the file specified." 但是我总是收到"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)
}

在此处输入图片说明

Your settings.json is not in the same directory as your main.go . 您的settings.jsonmain.go不在同一目录中。 If you invoke either go run main.go , or go build . && ./app 如果您调用go run main.gogo build . && ./app go build . && ./app , the current path will be .../app/ which does not contain the settings.json file. go build . && ./app ,当前路径将是.../app/ ,其中不包含settings.json文件。

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). 尝试将您的settings.json文件复制到与您的应用相同的目录中,本地调用将起作用(但是,如果您从单独的目录运行,它将仍然失败)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM