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()
}
复制代码

 

posted on   金科许俊  阅读(407)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」



点击右上角即可分享
微信分享提示