如何优雅地在Golang中创建配置文件?

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

How to make config file in Golang elegantly?

问题

我是一个刚入门的Golang开发者。
我想编写一个程序来管理我的Redis实例,以便我可以使用特定的配置文件创建Redis连接。但是我不知道如何优雅地创建Redis实例的配置文件。

我之前找到了"text/template",这是一个好主意吗?

英文:

I'm a newbie with Golang.
I want to write a program to manage my Redis instances so that I can create a Redis connection with specific config file. But I don't know how to create the config file for Redis instances elegantly.

I found "text/template" before, is that a good idea?

答案1

得分: 8

这取决于您要支持的配置文件格式。

一个能够读取大多数格式(从简单的ini文件到JSON文件)的库是spf13/viper

> Viper是Go应用程序的完整配置解决方案。它被设计为在应用程序内部处理各种类型的配置。它支持:

> - 设置默认值

  • 从yaml、toml和json配置文件中读取
  • 从环境变量中读取
  • 从远程配置系统(Etcd或Consul)中读取
  • 从命令行标志中读取
  • 设置显式值
英文:

It depends on the file format you want to support for those configs.

One library able to read most of those format (from a simple ini file to a JSON one) would be spf13/viper:

> Viper is a complete configuration solution for go applications. It has been designed to work within an application to handle all types of configuration. It supports

> - setting defaults

  • reading from yaml, toml and json config files

  • reading from environment variables

  • reading from remote config systems (Etcd or Consul)

  • reading from command line flags

  • setting explicit values

答案2

得分: 4

Redis配置文件采用简单的文本格式。您可以使用fmt包生成配置文件:

fmt.Fprintf(w, "pidfile %s\n", pidFile)
fmt.Fprintf(w, "port %d\n", port)

其中,w是用于输出的io.Writer

另一个可行的选项是text/template包。给定以下模板:

pidfile {{.PidFile}}
port {{.Port}}

您可以使用以下代码执行它:

t.Execute(w, map[string]interface{}{
   "PidFile": pidFile,
   "Port": port,
})
英文:

Redis configuration files have a simple text format. You can generate a configuration file using the fmt package:

fmt.Fprintf(w, "pidfile %s\n", pidFile)
fmt.Fprintf(w, "port %d\n", port)

where w is io.Writer for the output.

The text/template package is also a viable option. Given the template

pidfile {{.PidFile}}
port {{.Port}}

you can execute it with

t.Execute(w, map[string]interface{}{
   "PidFile": pidFile,
   "Port": port,
})

答案3

得分: 3

如果你想为开发、测试、暂存和生产创建一个配置文件,我建议使用Go提供的// +build功能。


设置你的Go程序

你需要在config包中创建4个文件,如下所示:

src/program/config
               |
               |--config_dev.go
               |--config_test.go
               |--config_staging.go
               |--config_prod.go

在配置文件中

然后在每个文件中,你需要定义用于在go build(或运行等)过程中使用该文件的标签。

例如,在config_dev.go中:

// +build dev

package config

// 基于构建标签的开发就绪配置常量。
const (
    MYSETTINGS = "dev blablabla"
    ISREADY    = false
)

config_test.go中,会是这样的:

// +build test

package config

// 基于构建标签的测试就绪配置常量。
const (
    MYSETTINGS = "test blablabla"
    ISREADY    = true
)

请注意// +build dev// +build test

这些是你在构建过程中要使用的标签,用于指定要使用哪个配置文件。

在任何其他Go文件中,你只需要调用config.ISREADY,而无需导入除"config"之外的任何其他内容。


构建

然后,要构建你的应用程序,只需运行:

go build -tags dev 以使用_开发配置_进行构建

或者

go build -tags test 以使用_测试配置_进行构建

英文:

If you want to make a config file for development, testing, staging and production, I would recommend to use the // +build possibility offered by Go.


Set up your Go program

You create 4 files in a config packages as followed :

src/program/config
               |
               |--config_dev.go
               |--config_test.go
               |--config_staging.go
               |--config_prod.go

In the config files

Then in each file, you define the tag used to use that file during the go build (or run, ...) process.

It means for instance in config_dev.go :

// +build dev

package config

// Development ready configuration const based on the build tags.
const (
    MYSETTINGS = "dev blablabla"
    ISREADY    = false
)

In the config_test.go, that would look like :

// +build test

package config

// Development ready configuration const based on the build tags.
const (
    MYSETTINGS = "test blablabla"
    ISREADY    = true
)

Note the // +build dev and // +build test.

That are the tags you are going to use during build to tell which config file you want to use.

In any other Go file, you just have to call config.ISREADY without importing anything else that "config" in your file.


Build

Then to build your app, you just have to run :

go build -tags dev to build with the development config

or

go build -tags test to build with the testing config.

答案4

得分: 1

由于Redis配置文件的结构非常简单,我建议你查看encoding/csv包,并将Reader.Comma分隔符设置为空格。这样可以轻松地读取、解析和写入配置。对我来说,“slaveof {{.Host}} {{.Port}}”作为模板看起来不太方便。但这肯定是正确的方法,只是个人口味不同而已。

英文:

As redis config file has very simple structure I'd suggest you to look at encoding/csv package with Reader.Comma delimiter set just to blank space. It allow you to both read, parse and write configuration easily. Seems to me "slaveof {{.Host}} {{.Port}}" as template looks not very handy. But it's sure correct approach. Just matter of taste.

答案5

得分: -1

我建议使用一些配置库。我喜欢Viper,因为它非常完整。

英文:

I would suggest to use some configuration library. I like Viper for is completeness.

huangapple
  • 本文由 发表于 2015年1月28日 02:00:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/28176981.html
匿名

发表评论

匿名网友

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

确定