【问题标题】:Golang access raw Podio field valuesGolang 访问原始 Podio 字段值
【发布时间】:2016-07-12 20:59:05
【问题描述】:

嗨,Podio 人(也许更具体地说是 Andreas),

我正在尝试更深入地研究 Golang API 库,但遇到了我的新手 Golang 技能。

在拨打client.getItems(...) 电话后,我希望遍历项目内部的字段并仅获取相关部分。最终目标是我可以像这样创建一个非常简化的 json 对象

{
    1000: "John", // key = app field id, value = text
    5490: [{item_id: 4031294, app_id: 94392}],  // relations
    5163: [1,2,5] // categories
}

但是我似乎无法掌握嵌套的item.Fields Values struct {}。我尝试使用反射,但没有任何运气。

有人可以帮我完成这段代码吗?

for _, field := range item.Fields {
  switch field.PartialField.Type {
  case "text":
    simpleValue := field.Values.Value // not working as I can't access Value in struct {}
  }
}

您好, PJ

【问题讨论】:

标签: api go struct podio


【解决方案1】:

试试type assertion

myTexts := field.Values.([]TextValue)

你还可以检查一个有效的断言,这样你的程序就不会恐慌

 myTexts, assertionSucceeded := field.Values.([]TextValue)

【讨论】:

  • 您好本杰明,感谢您的快速回复。使用您的第一个 sn-p,我得到 error: interface conversion: interface is []interface {}, not []podio.TextValue,而第二个它静默失败,myTexts 没有任何值
  • 顺便说一句,field.Values.([]interface{}) 确实有效,但我不知道如何将values 从地图中删除。
  • 尝试对断言结果使用 for 循环
  • 我试过for _, myText := range myTexts {,但没有成功。你有什么其他的语法建议?
  • 你在说什么地图?还是您的意思是“切片”?也许您也可以创建一个新问题,因为我们这里没有任何上下文。例如myTexts 是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 2019-08-04
  • 2014-03-14
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多