Writing the Software

With the basic functionality in place, it’s time to improve the program to be more useful. You’ll make the program more robust by adding a configuration file, expose the door state using a HTTP API, and send a Discord notification in case you forgot you left the door open at night.

Start by creating a directory for this new version, and initializing the Go module:

 $ ​​mkdir​​ ​​final
 $ ​​cd​​ ​​final
 $ ​​go​​ ​​mod​​ ​​init​​ ​​doorcheck
 go: creating new go.mod: module doorcheck

Now add the code to read a configuration file in yaml format. Use the external package yaml.v3[57] for this. Add the code to file config.go:

 package​ main
 
 import​ (
 "errors"
 "log"
 "os"
 

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.