Google App Engine-从应用程序代码读取JSON文件

问题描述

在我部署的一个应用程序引擎中,出现以下错误

error loading config file: open go-config.json: no such file or directory

这是handlers.go中的go代码

config := config{}
err := gonfig.GetConf("go-config.json",&config)
if err != nil {
    log.Printf("error loading config file: %v\n",err)
}

这是我的app.yaml文件

runtime: go114
service: finance

在应用程序引擎的调试器上,我看到项目结构看起来像这样。

app engine:/
quantify
    app.yaml
    config.go
    go-config.json
    main.go
    handlers.go
    quantify

为什么我不能在已部署的应用程序代码中读取此文件?我曾尝试以多种方式在app.yaml中指定处理程序,但均未成功。

解决方法

首先,您应该通过fmt.Println(os.Getwd())找出当前路径

fmt.Println(os.Getwd())
config := config{}
err := gonfig.GetConf("go-config.json",&config)
if err != nil {
    log.Printf("error loading config file: %v\n",err)
}

然后将“ go-config.json”更改为当前路径的相对路径