和 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) }
![]() | 1 janxin 2019-11-22 11:20:58 +08:00 ![]() Pipeliner 是 interface 啊,你加*做什么... |
2 zhangjiale OP @janxin 老哥强啊,不加 * 就可以了 |
3 zhangjiale OP 抱歉,犯蠢了,刚学 golang 不久 |
![]() | 5 zunceng 2019-11-22 11:57:57 +08:00 我感受到 github 对面传来的一股哀怨 /捂脸 |
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 } |