英文:
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.go
、config.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.go
和 config.json
的文件夹中运行 go build <your_package>
,它很可能会正常工作。
如果你使用的是 go install <your_package>
,你的可执行文件将会位于 $GOPATH/bin
中。在这种情况下,你需要相应地更新 ioutil.ReadFile()
函数调用的参数。
更好的方法是将 config.json
的文件位置作为参数传递给你的可执行文件,而不是硬编码它。
英文:
Depending on whether you use go install <your_package>
or go build <your_package>
, your final executable will end up in different location.
If you are using go build <your_package>
, your executable will be located in the folder where you invoked the command. IOW, it will likely work if your run go build <your_package>
in the folder that contains your main.go
and config.json
.
If you use go install <your_package>
, 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论