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”更改为当前路径的相对路径

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...