package config import ( "gopkg.in/yaml.v2" "os" "sync" ) var ( instance *config once sync.Once ) func init() { once.Do(func() { var conf config path, _ := os.Getwd() filePath := path + "/config/config.yaml" if f, err := os.Open(filePath); err != nil { panic(err) } else { err := yaml.NewDecoder(f).Decode(&conf) if err != nil { panic(err) } } instance = &conf }) } //获取配置文档实例 func Instance() *config { return instance } type config struct { Server server `yaml:"server"` Database database `yaml:"database"` Logger logger `yaml:"logger"` } type server struct { Address string `yaml:address"` } type database struct { Host string `yaml:"host"` User string `yaml:"user"` Password string `yaml:"password"` Port string `yaml:"port"` Name string `yaml:"name"` Timezone string `yaml:"timezone"` } type logger struct { Path string Level string Stdout bool }