Created
September 1, 2014 01:15
-
-
Save codingjester/31b2232dee13de5340b3 to your computer and use it in GitHub Desktop.
Well, Now we're getting complicated. Hardcoded values for a database are kinda useless right? Lets load some JSON configs.
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
var config *Configuration // A global we'll reuse for the application | |
// Defining the structure of the JSON as we'll expect it | |
type Configuration struct { | |
Port int // Port the app is served on | |
Db_Type string | |
Db_Username string | |
Db_Password string | |
Db_Host string | |
DB string | |
} | |
// Loads configurations from a JSON file in a config directory relative to the app | |
func loadConfig() { | |
file, err := ioutil.ReadFile("config/config.json") | |
if err != nil { | |
log.Fatal("unable to open config: ", err) | |
} | |
temp := new(Configuration) // Get a pointer to an instance with new keyword | |
// Unmarshal is going to decode and store into temp | |
if err := json.Unmarshal(file, temp); err != nil { | |
log.Fatal("unable to marshal JSON: ", err) | |
} | |
config = temp | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment