【发布时间】:2018-10-03 13:37:40
【问题描述】:
我将 MacOS Mojave 与 IntelliJ Ultimate 2018.2 一起使用。
我编写了一个 go 应用程序,它从我的项目目录中加载一个配置文件。我创建了一个 InitConfig() 函数来将 JSON 文件加载到结构中。这是我的Config.go 文件的代码
package main
import (
"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 Configuration
func 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 main
import (
"log"
"os"
"path/filepath"
)
var ExPath string
func 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知道它应该在执行我的项目之前复制它?
谢谢!
【问题讨论】:
-
os.Getwd(),为您提供当前工作目录,cwd和executable path是不同的东西,您是否首先创建了配置文件。 -
@nilsocket - 我不能使用 Getwd 函数,因为我不想使用工作目录,以防我从不同的目录执行文件,并且由于某种原因 Getwd 返回的根目录我的项目,而不是我在运行/调试配置文件中设置的工作目录
-
谁在创建配置文件,如果是你,那么你可以考虑固定路径,你不会在
InitConfig()中调用filepath.Dir()。尝试用最少且完整的示例报告您的问题会令人困惑 -
@nilsocket - 感谢您提供所有有价值的信息。看来我只需要更好地配置我的运行/调试配置文件。
标签: go intellij-idea