8

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?

rohanagarwal
  • 771
  • 9
  • 30
Papulatus
  • 677
  • 2
  • 8
  • 18
  • 1
    possible duplicate of [How to handle configuration in Go](http://stackoverflow.com/questions/16465705/how-to-handle-configuration-in-go) – icza Jan 27 '15 at 18:41

5 Answers5

8

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
slier
  • 6,511
  • 6
  • 36
  • 55
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
4

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,
})
Charlie Tumahai
  • 113,709
  • 12
  • 249
  • 242
3

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.

Rem's
  • 442
  • 3
  • 8
1

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.

Uvelichitel
  • 8,220
  • 1
  • 19
  • 36
-1

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

fabrizioM
  • 46,639
  • 15
  • 102
  • 119