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

2019-11-22 10:55:11 +08:00
 zhangjiale

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 命令,

所以尝试 cast Interface

func test(pipe *redis.Pipeliner) {
    p := pipe.(*redis.Pipeline) // IDE 提示 invalid type assertion, (no interface-type *redis.Pipeline on left) 
}

3565 次点击
所在节点    Go 编程语言
6 条回复
janxin
2019-11-22 11:20:58 +08:00
Pipeliner 是 interface 啊,你加*做什么...
zhangjiale
2019-11-22 11:36:27 +08:00
@janxin 老哥强啊,不加 * 就可以了
zhangjiale
2019-11-22 11:38:28 +08:00
😳抱歉,犯蠢了,刚学 golang 不久
ahmcsxcc
2019-11-22 11:43:03 +08:00
@janxin #1
interface 为什么不能加*?
求解
我也是刚学 go
zunceng
2019-11-22 11:57:57 +08:00
我感受到 github 对面传来的一股哀怨 /捂脸
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
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/622039

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX