dougu1985 2017-12-07 19:56
浏览 153
已采纳

Go找不到指定的文件

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

  • 写回答

1条回答 默认 最新

  • dtsps2098 2017-12-07 20:10
    关注

    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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码