github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/mock/database/loader.go (about) 1 package database 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "gopkg.in/yaml.v2" 7 "io/ioutil" 8 "os" 9 ) 10 11 func LoadAndParse(path string, v interface{}) error { 12 var content []byte 13 file := path + ".json" 14 _, err := os.Stat(file) 15 if err == nil { 16 content, err = ioutil.ReadFile(file) 17 if err != nil { 18 return err 19 } 20 21 return json.Unmarshal(content, v) 22 } 23 24 file = path + ".yml" 25 _, err = os.Stat(file) 26 if err == nil { 27 content, err = ioutil.ReadFile(file) 28 if err != nil { 29 return err 30 } 31 32 return yaml.Unmarshal(content, v) 33 } 34 35 return fmt.Errorf("only support .json or .yml") 36 }