V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
zhangjiale
V2EX  ›  Go 编程语言

[求助] go-redis, 如何将 pipeline 传入函数

  •  
  •   zhangjiale · 2019-11-22 10:55:11 +08:00 · 3553 次点击
    这是一个创建于 1609 天前的主题,其中的信息可能已经有所发展或是发生改变。

    issues #1164 遇到的问题一样

    我的目的

    将 pipeline 传入其他函数后,还能正常使用 redis 命令

    func main() {
            client := redis.NewClient(&redis.Options{
    		Addr:     "127.0.0.1:6379",
    		Password: "", 
    		DB:       0, 
    	})
        pipe := client.Pipeline()       // 1. 现在 pipe 的类型是 Pipeliner interface
        pipe.Hset("yo", "key", "test")  // 2. 在这里可以正常使用 redis 命令
        test(&pipe)
        pipe.Exec()
    }
    
    func test(pipe *redis.Pipeliner) {
          pipe.HSet("yo", "key", "test") // 3. 在这里就不行了, 现在的 pipe 一个 method 都没有
    }
    

    已经尝试过的方法

    因为 redis.Pipeline type 可以直接使用 redis 命令,

    redis.Pipeline 的补全.png

    所以尝试 cast Interface

    func test(pipe *redis.Pipeliner) {
        p := pipe.(*redis.Pipeline) // IDE 提示 invalid type assertion, (no interface-type *redis.Pipeline on left) 
    }
    
    
    6 条回复    2019-11-22 12:23:24 +08:00
    janxin
        1
    janxin  
       2019-11-22 11:20:58 +08:00   ❤️ 2
    Pipeliner 是 interface 啊,你加*做什么...
    zhangjiale
        2
    zhangjiale  
    OP
       2019-11-22 11:36:27 +08:00
    @janxin 老哥强啊,不加 * 就可以了
    zhangjiale
        3
    zhangjiale  
    OP
       2019-11-22 11:38:28 +08:00
    😳抱歉,犯蠢了,刚学 golang 不久
    ahmcsxcc
        4
    ahmcsxcc  
       2019-11-22 11:43:03 +08:00
    @janxin #1
    interface 为什么不能加*?
    求解
    我也是刚学 go
    zunceng
        5
    zunceng  
       2019-11-22 11:57:57 +08:00
    我感受到 github 对面传来的一股哀怨 /捂脸
    achenme
        6
    achenme  
       2019-11-22 12:23:24 +08:00
    Pipeliner 传递过来的 interface 是指针,在对他取址,就找不到方法了

    func (c *Client) Pipeline() Pipeliner {
    pipe := Pipeline{
    ctx: c.ctx,
    exec: c.processPipeline,
    }
    pipe.init()
    return &pipe
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1416 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 23:52 · PVG 07:52 · LAX 16:52 · JFK 19:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.