dccmmtop / notebook

个人博客记录
0 stars 0 forks source link

yml配置文件读取 #83

Open dccmmtop opened 2 years ago

dccmmtop commented 2 years ago
import (
    "fmt"
    "github.com/spf13/viper"
    "os"
    "path/filepath"
)

type Config struct  {
    DesDir string
    SourceFiles []string
}
func loadConfig()(con Config){
    home := os.Getenv("HOME")
    viper.SetConfigFile(filepath.Join(home,"config","syncFile.yml"))
    viper.SetConfigType("yml")
    err := viper.ReadInConfig()
    checkErr(err)
    err = viper.Unmarshal(&con)
    checkErr(err)
    fmt.Printf("config: %v\n", con)
    return con
}
func checkErr(err error){
    if err != nil {
        panic(err)
    }
}