Skip to content

Instantly share code, notes, and snippets.

@swedishborgie
Created January 2, 2019 19:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swedishborgie/952971aed765153f2df97004aeb0fe21 to your computer and use it in GitHub Desktop.
Save swedishborgie/952971aed765153f2df97004aeb0fe21 to your computer and use it in GitHub Desktop.
Micro go-config issue example
package main
import (
"io/ioutil"
"log"
"time"
config "github.com/micro/go-config"
)
func overwriteConfig(flip bool) {
json := `{"hosts":{"database":{"address":"10.0.0.1","port": 3306}}}`
if flip {
json = `{"hosts":{"database":{"address":"example.com","port": 3306}}}`
}
log.Printf("Overwriting configuration: %s\n", json)
ioutil.WriteFile("config.json", []byte(json), 0644)
}
func main() {
// Establish initial state.
overwriteConfig(false)
// Load configuration.
config.LoadFile("config.json")
// Load initial value.
var val interface{}
config.Get("hosts", "database", "address").Scan(&val)
log.Printf("Initial value: %+v\n", val)
// Set up watch
w, err := config.Watch("hosts", "database", "address")
if err != nil {
log.Fatalf("Problem configuring watch: %s", err)
}
// Queue up a change.
go func() {
time.Sleep(5 * time.Second)
overwriteConfig(true)
}()
// Wait for changes.
for {
newVal, err := w.Next()
if err != nil {
log.Fatalf("Problem getting next watch: %s", err)
}
log.Printf("Got a change!\n")
err = newVal.Scan(&val)
if err != nil {
log.Fatalf("Problem getting new value: %s", err)
}
log.Printf("New value: %+v\n", val)
}
}
$ go run main.go
2019/01/02 14:48:10 Overwriting configuration: {"hosts":{"database":{"address":"10.0.0.1","port": 3306}}}
2019/01/02 14:48:10 Initial value: 10.0.0.1
2019/01/02 14:48:15 Overwriting configuration: {"hosts":{"database":{"address":"example.com","port": 3306}}}
2019/01/02 14:48:15 Problem getting next watch: invalid character 'e' looking for beginning of value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment