doutang6600 2017-04-07 14:01
浏览 23
已采纳

Golang RethinkDB ChangeFeed结构

I was wondering if someone could explain how to unmarshal my changefeed cursor value to a specific struct type.

var message map[string]interface{}

for chatFeedCursor.Next(&message) {
    fmt.Println(message)
}

map[new_val:map[club_id:ea2eb6e2-755f-4dad-922d-e3693b6e55c6 date:2017-04-07 14:48:17.714 +0100 +01:00 id:e389ab54-963e-4b33-9b34-adcb6ec5b17e message:what is the meaning of life? user_id:00ff679f-9421-4b8b-ae7f-d11cf2adaee2] old_val:]

However, I would like the response to be mapped to struct ChatMessage.

Update:

I've tried:

var message ChatMessage

However, it doesn't seem like any of my data gets set in the struct.

{ 0001-01-01 00:00:00 +0000 UTC}

My struct:

type ChatMessage struct {
    ID      string        `json:"id" gorethink:"id,omitempty"`
    UserID      string    `json:"user_id" gorethink:"user_id"`
    ClubID      string    `json:"club_id" gorethink:"club_id"`
    Message     string    `json:"message" gorethink:"message"`
    Date    time.Time     `json:"date" gorethink:"date"`
}

Thanks.

  • 写回答

1条回答 默认 最新

  • donglieshe4692 2017-04-07 14:18
    关注

    I figured it out!

    The problem was that I didn't specify a field on the rethinkdb change request.

    Previous code:

    chatFeedCursor, _ := gorethink.Table("club_chat").Changes().Run(gorethinkSession)
    

    Working Code:

    chatFeedCursor, _ := gorethink.Table("club_chat").Changes().Field("new_val").Run(gorethinkSession)
    

    Now the .Next() value maps to my struct with no issues.

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

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)