英文:
Setting Values in Go Application at Runtime
问题
我正在编写一个使用Go语言的应用程序,该程序将值写入文件。
然而,我希望文件的位置在运行时确定(即不作为应用程序代码的一部分设置)。
我已经为应用程序创建了一个测试客户端,我希望客户端告诉应用程序它应该写入哪个文件。
有人可以告诉我如何在Go语言中实现这个功能吗?
谢谢,
Sean
英文:
I'm writing a go application that writes values to a file.
However, I want the location of that file to be determined at runtime (i.e. not set as part of the application code).
I have create a test client for the application and I want the client to tell the application which file it should write to.
Can anyone tell me how I would go about this in Go?
Thanks,
Sean
答案1
得分: 1
如果位置未设置,则需要以某种方式读取它。
- 您可以在运行命令时从命令行标志中读取位置。最简单的方法是使用flag包。
- 您可以从标准输入中读取位置。这个问题的最佳答案很好地解释了如何做到这一点。
- 您可以从环境变量中读取位置。请查看os包中的Environ函数。
英文:
If the location is not set, you'll need to read it in some way.
- You might want to read it from a command line flag passed in when running the command. The easiest way to do that is using the flag package.
- You could read it from STDIN. The top answer to this question nicely explains how to do that.
- You could read it from an environment variable. Check out the Environ function in the os package.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论