首页 > 解决方案 > Google App Engine - 从应用程序代码中读取 JSON 文件

问题描述

在我部署的一个应用程序引擎中,我收到以下错误。

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

这是 go 代码的片段handlers.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以多种方式指定处理程序,但均未成功。

标签: gogoogle-app-enginegoogle-cloud-platformenvironmentapp.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”更改为与当前路径相关的路径


推荐阅读