如何在golang rethinkdb驱动程序gorethink中重命名结果字段?

时间:2016-04-20 05:39:50

标签: go rethinkdb

r.table('customers')
  .map(function(purchase) {
      return {zip:customer('address')('zip'), product:purchase('name')};
    })      
 .run(...)

我如何代表map({"new_column_name":Row.Field("original_column_name")}) gorethink

感谢任何帮助...

1 个答案:

答案 0 :(得分:2)

我只是将你的node.js代码(在你以前的版本中)移植到golang:

  session, err := r.Connect(r.ConnectOpts{
    Address: "localhost:28015",
  })
  if err != nil {
    return
  }

  res, err := table.ConcatMap(func(customer r.Term) interface{} {
    return customer.Field("purchases").Map(func(purchase r.Term) interface{} {
      return map[string]interface{}{
        "zip":     customer.Field("address").Field("zip"),
        "product": purchase.Field("name"),
      }
    })
  }).Run(session)

  if err != nil {
    return
  }

  defer res.Close()

  var response interface{}
  for res.Next(&response) {
    fmt.Println(response)
  }