I use this same configuration structure and supporting functions in most every Go program I work on. It is pretty simple but makes it easy to keep global variables orgranized and configurable.
Here is what I do:
-
Create a
type config struct
-
Every configurable variable for the program becomes a Member field of the Configuration structure.
-
Register every field of the Configuration Structure with go’s builtin flags package such that the flags are parsed when the program starts we can use our configuration variable.
import (
"flags"
)
typedef Configuration struct {
Broker string `json:"broker"`
}
var (
config Configuration
)
func init() {
flags.StringVar(&config.Broker, "broker", "localhost", "Set the MQTT Broker")
}
func main() {
flags.Parse()
mqtt_init(config.Broker)
}
func (c *Configuration) Save(fname string) error {
// encode configuration into a JSON struct
// save json string to file
}
func (c *Configuration) Read(fname string) error {
// Read json string from file
// decode configuration from the JSON struct.
}
Adding the Save() and Read() functions now allow us to read and write our configuration to and from a file.