Files
learn-golang/Web开发/04go-viper-demo/05viper_watch.go
liumangmang b010f82221 feat(auth): 添加完整的用户认证API项目
- 实现用户注册、登录、JWT令牌认证功能
- 集成Gin、GORM、Viper、Zap等框架
- 添加密码加密、数据库操作、中间件等完整功能
- 配置多环境支持、日志轮转、CORS处理
- 创建完整的项目结构和配置文件体系
2025-12-30 18:00:42 +08:00

40 lines
940 B
Go

package main
import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
func main() {
// 读取配置文件
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
fmt.Println("Initial config loaded")
fmt.Printf("App Name: %s\n", viper.GetString("app.name"))
fmt.Printf("App Port: %d\n", viper.GetInt("app.port"))
// 监听配置文件变化
viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
fmt.Println("\n--- Config file changed! ---")
fmt.Printf("Event: %s\n", in.Op.String())
fmt.Printf("File: %s\n", in.Name)
fmt.Printf("App Name: %s\n", viper.GetString("app.name"))
fmt.Printf("App Port: %d\n", viper.GetInt("app.port"))
})
fmt.Println("\nWatching for config changes... (modify config.yaml to see changes)")
fmt.Println("Press Ctrl+C to exit")
// 保持程序运行
select {}
}