How make global json config and use it everywhere?
func indexHandler(w http.ResponseWriter, r *http.Request) {
// Use config
fmt.Println(config["Keywords"]) // <-- USE HERE
}
func main() {
config := models.Conf() // Init there!
fmt.Println(config.Keywords) // This prints "keywords1" - good
// Routes
http.HandleFunc("/", indexHandler)
// Get port
http.ListenAndServe(":3000", nil)
}
Full code: https://gist.github.com/liamka/15eec829d516da4cb511
The problem is simply that in main you create a new config instance instead of using the global variable
You have:
var config map[string]*models.Config
Which is the global var. and in main() you have:
func main() {
config := models.Conf()
...
which creates a local variable and throws it away. This is what you need to do:
The global var:
var config models.Config
In main:
func main() {
config = models.Conf()
...
this will reference the global variable and not the local one.