How to make config file in Golang elegantly?


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?


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,

Answered By – Bayta Darell

Answer Checked By – David Goodson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.