Created
January 2, 2019 19:53
-
-
Save swedishborgie/952971aed765153f2df97004aeb0fe21 to your computer and use it in GitHub Desktop.
Micro go-config issue example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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