Created
July 13, 2020 18:00
-
-
Save aavrug/962a6649a45b60e67642223b799af002 to your computer and use it in GitHub Desktop.
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
// Config.go | |
package config | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
) | |
func init() { | |
cfg = new(Config) | |
} | |
var cfg *Config | |
type Config map[string]DiscoverConfig | |
// DiscoverConfig struct | |
type DiscoverConfig struct { | |
Host string `json:"host,omitempty"` | |
Port string `json:"port,omitempty"` | |
} | |
// GetConfig method | |
func GetConfig() *Config { | |
return cfg | |
} | |
// LoadConfig loads | |
func LoadConfig(path string) error { | |
data, err := ioutil.ReadFile(path) | |
if err != nil { | |
return nil | |
} | |
err = json.Unmarshal(data, cfg) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
// main.go | |
func main() { | |
if err := cfg.LoadConfig(*configfile); err != nil { | |
log.Print("load condig file failed : ", err.Error()) | |
return | |
} | |
log.Print("start init default server", cfg.GetConfig()) | |
config := cfg.GetConfig() | |
// fmt.Printf("%#v\n", users) | |
fmt.Println(config[todo].Host) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment