github.com/Anderson-Lu/gobox@v0.0.0-20191127065433-3e6c4c2da420/config/config_helper.go (about) 1 package config 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "io/ioutil" 7 8 yaml "gopkg.in/yaml.v2" 9 ) 10 11 const ( 12 CONFIG_JSON_FORMAT int = 0 13 CONFIG_YAML_FORMAT int = 1 14 ) 15 16 //加载配置信息 17 func InitConfigFile(path string, format int, value interface{}) error { 18 19 file, err := ioutil.ReadFile(path) 20 if err != nil { 21 return err 22 } 23 24 switch format { 25 case CONFIG_JSON_FORMAT: 26 return json.Unmarshal(file, &value) 27 case CONFIG_YAML_FORMAT: 28 e := yaml.Unmarshal(file, value) 29 return e 30 default: 31 return fmt.Errorf("invalid format type: %d", format) 32 } 33 }