Configuring the Application Settings

To make your program more flexible, let’s add a feature that allows you to specify required parameters using a configuration file in YAML format. This way, if the conditions change, you can update the configuration files without recompiling your application. Create a file config.go that’s responsible for importing the key values stored in the config.yml file, like this:

 package​ main
 
 import​ (
 "errors"
 "fmt"
 "os"
 "sort"
 
  hue ​"github.com/collinux/gohue"
 "gopkg.in/yaml.v3"
 )
 
 var​ errInvalidColor = errors.New(​"invalid color"​)
 
 type​ color ​struct​ {
  Color ​string​ ​`yaml:"color"`
  Threshold ​int​ ​`yaml:"threshold"`
 }
 
 var​ colorTranslate ...

Get Automate Your Home Using Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.