package svc import ( // 引入配置文件 "backend/usercenter/rpc/internal/config" // 引入GORM "gorm.io/driver/mysql" "gorm.io/gorm" // 引入redis "fmt" "github.com/redis/go-redis/v9" ) // 服务上下文 type ServiceContext struct { Config config.Config UsercenterDB *gorm.DB RedisClient *redis.Client // 注意类型 } func NewServiceContext(c config.Config) *ServiceContext { usercenterDB, err := gorm.Open(mysql.Open(c.MySQL.DSN()), &gorm.Config{}) if err != nil { panic(err) } redisClient := redis.NewClient(&redis.Options{ Addr: fmt.Sprintf("%s:%d", c.RedisGo.Host, c.RedisGo.Port), Password: c.RedisGo.Pass, // 或 Password,取决于你的 config.go/yaml DB: c.RedisGo.DB, }) return &ServiceContext{ Config: c, UsercenterDB: usercenterDB, RedisClient: redisClient, } }