亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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 關注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號