Last active
August 29, 2015 14:26
-
-
Save liamka/15eec829d516da4cb511 to your computer and use it in GitHub Desktop.
JSON from func make it global
This file contains hidden or 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 models | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
) | |
type Config struct { | |
Keywords string `json:"keywords"` | |
Social []struct { | |
Url string `json:"url"` | |
Title string `json:"title"` | |
} `json:"social"` | |
} | |
func Conf() Config { | |
var с Config | |
configFile, _ := ioutil.ReadFile("config.json") | |
json.Unmarshal([]byte(configFile), &с) | |
return с | |
} |
This file contains hidden or 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
{ | |
"keywords": "keywords1", | |
"social": [ | |
{"url": "test1", "title": "test1"}, | |
{"url": "test2", "title": "test2"} | |
], | |
"mysql": "123123123123" | |
} |
This file contains hidden or 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 ( | |
"fmt" | |
"net/http" | |
"./models" | |
) | |
// Global variables | |
var config map[string]*models.Config | |
func indexHandler(w http.ResponseWriter, r *http.Request) { | |
// Use config | |
fmt.Println(config["Keywords"]) // Prints nil - why? | |
} | |
func main() { | |
config := models.Conf() | |
fmt.Println(config.Keywords) // Prints "keywords1" | |
// Routes | |
http.HandleFunc("/", indexHandler) | |
// Get port | |
http.ListenAndServe(":3000", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment