为了账号安全,请及时绑定邮箱和手机立即绑定

使用 intellij 运行 go 应用程序时加载配置文件

使用 intellij 运行 go 应用程序时加载配置文件

Go
九州编程 2023-04-17 16:09:05
我将 MacOS Mojave 与 IntelliJ Ultimate 2018.2 结合使用。我写了一个 go 应用程序,它从我的项目目录加载一个配置文件。我创建了一个InitConfig()函数来将 JSON 文件加载到结构中。Config.go这是我文件的代码package mainimport (    "github.com/tkanos/gonfig"    "log")type Configuration struct {    WebServerPort   int    DbName string    DbUser string    DbPassword string    DbPort int    CertFile string    KeyFile string    EnableHttps bool}var AppConfiguration Configurationfunc InitConfig() {    AppConfiguration = Configuration{}    err := gonfig.GetConf(ExPath + "/config/config.json", &AppConfiguration)    if err != nil {        log.Fatalf("could not parse configuration file: %v",err)    }}我使用一个名为的变量ExPath,其中包含正在运行的二进制文件的当前目录,我使用以下代码 (Path.go) 创建了它:package mainimport (    "log"    "os"    "path/filepath")var ExPath stringfunc InitPath() {    ex, err := os.Executable()    if err != nil {        log.Fatalf("could not get executable path: %v",err)    }    ExPath = filepath.Dir(ex)}当我尝试运行或调试应用程序时,intellij 正在创建一个编译和运行应用程序的目录,我如何告诉它也复制 json 文件?当我尝试运行/调试我的应用程序时,我得到:could not parse configuration file: open /private/var/folders/60/qzt2pgs173s4j_r6lby_mw1c0000gn/T/config/config.json: no such file or directory我检查了一下,那个目录确实不包含我的配置目录。所以..有没有办法让intellij知道它应该在执行我的项目之前复制它?
查看完整描述

1 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

看起来解决方案很简单。我所需要的只是将我的运行/调试配置的“输出目录”设置为我的项目目录。然后 intellij 在同一目录中执行该文件,它可以找到/config/config.json.



查看完整回答
反对 回复 2023-04-17
  • 1 回答
  • 0 关注
  • 60 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信