dougai0138 2015-10-04 09:07
浏览 18
已采纳

如何在gorethink中使用r.Do和r.Branch? [关闭]

I'm looking for clear example of r.Do() and r.Branch() functions in gorethink.

  • 写回答

1条回答 默认 最新

  • drnf593779 2015-10-05 03:30
    关注

    The author of Go RethinkDB driver is very active on Github and you may get quicker answer if asking over there.

    Here is an example of Do.

        res, _ := r.DB("test").Table("table").Get("ID").Do(func(issue r.Term) r.Term {
            return issue.Merge(map[string]string{
                "new_field": "new_value",
            })
        }).Run(session)
        var b []interface{}
        a.All(&b)
        fmt.Println(b)
    

    I think the most important thing is to ensure we declare correct type Term. I import RethinkDB driver as r so I used r.Term. Then inside Do function, you can use any command from Term: https://godoc.org/github.com/dancannon/gorethink#Term

    An example of Do and Branch at same time:

        r.DB("test").Table("issues").Insert(map[string]interface{}{
            "id": 1111,
        }).Do(func(result r.Term) r.Term {
            return r.Branch(result.Field("inserted").Gt(0),
                r.DB("test").Table("log").Insert(map[string]interface{}{
                    "time":   r.Now(),
                    "result": "ok",
                }),
                r.DB("test").Table("log").Insert(map[string]interface{}{
                    "time":   r.Now(),
                    "result": "failed",
                }))
        }).Run(session)
    

    Basically, inside Do, we have access to return value of previous funciton call of RethinkDB, and we can continue to call ReQL command on that return value as in a Map function.

    Inside Branch, we pass 3 arguments, all are Term type in Go RethinkDB driver. You can also refer to the official example: https://godoc.org/github.com/dancannon/gorethink#Branch

    It's bit hard to get familiar with Go driver because of the nature of language and cannot fit in same dynamic style of official driver(Ruby, Python, JS). It's even more confusing in Language that we cannot chain command.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解答,怎么数码管中这么加入闹钟或者传感器,这应该怎么加入相应的代码
  • ¥15 scottplot5
  • ¥30 想问问这个建模怎么编程没有思路
  • ¥15 关于imageENview(ImageEN)中新建图层并根据鼠标位置添加图标
  • ¥100 用两台电脑局域联网进行MT5的EA参数优化,但是连接不上
  • ¥15 FastAPI报错: AsyncSession不是有效Pydantic类型
  • ¥50 这Mac系统提示虚拟内存不足,怎么解决
  • ¥15 Rs232电路无法收发数据,求帮助
  • ¥15 百度cookie扫码登录器
  • ¥15 微机原理汇编语言debug调试实验