Skip to content

Day 20 - 配置管理与日志

Viper配置管理

bash
go get github.com/spf13/viper
go
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")

var config Config
viper.Unmarshal(&config)

日志库logrus

bash
go get github.com/sirupsen/logrus
go
import log "github.com/sirupsen/logrus"

log.SetFormatter(&log.JSONFormatter{})
log.SetLevel(log.InfoLevel)

log.Info("信息日志")
log.Warn("警告日志")
log.Error("错误日志")

📅 学习进度

Day主题状态
01-19基础
20配置与日志
21RESTful API

本文为Go 30天学习计划第20天内容

📚 导航

| ← Day 19 - 上一章 | Day 21 - 下一章 →

最后更新: