config.yml
app: host: 127.0.0.1 port: 3306 username: adminsdsd password: admin log: suffix: log maxSize: 5
main.go
package main import ( "fmt" "io/ioutil" "github.com/go-yaml/yaml" ) type Config struct { App *App `yaml:"app"` Log *Log `yaml:"log"` } type App struct { Host string `yaml:"host"` Port int `yaml:"port"` Username string `yaml:"username"` Password string `yaml:"password"` } type Log struct { Suffix string `yaml:"suffix"` MaxSize int `yaml:"maxSize"` } func InitConfig() { yamlFile, err := ioutil.ReadFile("./config/config.yml") if err != nil { fmt.Println(err.Error()) } var _config *Config err = yaml.Unmarshal(yamlFile, &_config) if err != nil { fmt.Println(err.Error()) } fmt.Printf("config.app: %#v\n", _config.App) fmt.Printf("config.log: %#v\n", _config.Log) } func main() { InitConfig() }