使用配置文件与编译的Go程序一起使用

huangapple go评论85阅读模式
英文:

Using a configuration file with a compiled Go program

问题

我有一个使用config.json文件获取一些信息的Go应用程序。当我运行go run main.go时,它可以正常工作,但是当我将应用程序编译成可执行文件时,出现了错误open config.json: no such file or directory

我的代码如下:

func main() {
    data, err := ioutil.ReadFile("./config.json")
    check(err)

    var config config
    err = json.Unmarshal(data, &config)
    check(err)
}

我还尝试过ioutil.ReadFile("config.json"),但是它不起作用。check(err)config结构体都在main.go中,问题不是出在这里。

main.goconfig.json和可执行文件都在同一个目录下。

在程序编译后如何使用config.json文件?

英文:

I have a Go application using a config.json file to get some information. When doing a go run main.go it works but when I'm compiling the application into an executable I have the error open config.json: no such file or directory.

My code is:

func main() {
        data, err := ioutil.ReadFile("./config.json")
        check(err)

        var config config
        err = json.Unmarshal(data, &config)
        check(err)
}

I also have tried ioutil.ReadFile("config.json") and it does not work. check(err) and the config structure are in the main.go, the problem does not come from here.

main.go, config.json and the executable are in the same directory.

What should I do to be able to use the config.json file once the program is compiled?

答案1

得分: 12

你的配置文件可能不在你启动应用程序的工作目录中。
但是硬编码文件路径并不是最佳实践。

方法一:命令行参数

使用flag包将配置文件的路径作为命令行参数传递:

var filename = flag.String("config", "config.json", "配置文件的位置。")

func main() {
    flag.Parse()
    data, err := ioutil.ReadFile(*filename)
    check(err)

    var config config
    err = json.Unmarshal(data, &config)
    check(err)
}

使用./application -config=/path/to/config.json(根据你的平台)启动应用程序。


方法二:环境变量

使用os包从系统环境中读取路径。

func main() {
    filename := os.Getenv("PATH_TO_CONFIG")
    if filename == "" {
        filename = "config.json"
    }

    data, err := ioutil.ReadFile(filename)
    check(err)

    var config config
    err = json.Unmarshal(data, &config)
    check(err)
}

设置环境变量export PATH_TO_CONFIG=/path/to/config.json(根据你的平台)并启动应用程序。

如果没有提供路径给应用程序,这两种方法都会尝试在工作目录中找到config.json文件。

英文:

Your config file is probably not in the working directory you have started your application in.
But hardcoding the path to the file is not the best of practices.

Approach #1: Command-line argument

Use the flag package to pass the path to your config file as a command-line flag:

var filename = flag.String("config", "config.json", "Location of the config file.")

func main() {
        flag.Parse()
        data, err := ioutil.ReadFile(*filename)
        check(err)

        var config config
        err = json.Unmarshal(data, &config)
        check(err)
}

Start the application with ./application -config=/path/to/config.json (depends on your platform).


Approach #2: Environment variable

Use the os package to read the path from your system environment.

func main() {
        filename := os.Getenv("PATH_TO_CONFIG")
        if filename == "" {
          filename = "config.json"
        }

        data, err := ioutil.ReadFile(filename)
        check(err)

        var config config
        err = json.Unmarshal(data, &config)
        check(err)
}

Set the environment variable export PATH_TO_CONFIG=/path/to/config.json (depends on your platform) and start the application.

Both approaches will attempt to find config.json in the working directory if no path was provided to the application.

答案2

得分: 4

根据你使用的是 go install <your_package> 还是 go build <your_package>,你的最终可执行文件将会位于不同的位置。

如果你使用的是 go build <your_package>,你的可执行文件将会位于你执行该命令的文件夹中。也就是说,如果你在包含 main.goconfig.json 的文件夹中运行 go build <your_package>,它很可能会正常工作。

如果你使用的是 go install <your_package>,你的可执行文件将会位于 $GOPATH/bin 中。在这种情况下,你需要相应地更新 ioutil.ReadFile() 函数调用的参数。

更好的方法是将 config.json 的文件位置作为参数传递给你的可执行文件,而不是硬编码它。

英文:

Depending on whether you use go install &lt;your_package&gt; or go build &lt;your_package&gt;, your final executable will end up in different location.

If you are using go build &lt;your_package&gt;, your executable will be located in the folder where you invoked the command. IOW, it will likely work if your run go build &lt;your_package&gt; in the folder that contains your main.go and config.json.

If you use go install &lt;your_package&gt;, your executable will be located in your $GOPATH/bin. In that case, you will have to update the argument to your ioutil.ReadFile() function call accordingly.

The better approach is to pass the file location of your config.json as an argument to your executable, instead of hard-coding it.

huangapple
  • 本文由 发表于 2016年2月16日 05:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/35419263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定