亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 go 中使用 struct 作為包裝器

在 go 中使用 struct 作為包裝器

Go
慕桂英3389331 2022-07-11 15:31:58
如何包裝redis.Client到我的struct? 我有那個代碼并給我一個錯誤package mainimport (    "github.com/go-redis/redis")var cache *RedisCache// RedisCache structtype RedisCache struct {    *redis.Client}func initCache() *RedisCache {    if cache == nil {        cache = redis.NewClient(&redis.Options{           Addr:     "localhost:6379",            Password: "",            DB:       0,        })    }    return cache}cannot use redis.NewClient(&redis.Options literal) (type *redis.Client) as type *RedisCache in assignment有一些方法可以轉換該屬性嗎?
查看完整描述

1 回答

?
暮色呼如

TA貢獻1853條經驗 獲得超9個贊

redis.NewClient()返回一個類型的值*redis.Client。使用復合文字來創建您的值,RedisCache您可以將其分配給cache:


func initCache() *RedisCache {

    if cache == nil {

        client := redis.NewClient(&redis.Options{

            Addr:     "localhost:6379",

            Password: "",

            DB:       0,

        })

        cache = &RedisCache{Client: client}

    }


    return cache

}


查看完整回答
反對 回復 2022-07-11
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號