我將 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
.
- 1 回答
- 0 關注
- 115 瀏覽
添加回答
舉報
0/150
提交
取消