dongpu1881 2019-02-25 20:01
浏览 440
已采纳

golang从接口{}获取值

I am iterating through the results returned from a couchDB.View and extracting the Key.

for _, row := range rows {
        fmt.Printf("%v, %T
", row.Key, row.Key)
    }

The result of this is:

[nh001 mgr], []interface {}
[nh002 nh], []interface {}

I need to read through this interface and get the 2nd value ("mgr" or "nh").

I am not able to iterate over this as this is not a map.

  • 写回答

1条回答 默认 最新

  • douchunji1885 2019-02-25 20:22
    关注

    I assume that your value row.Key is returned from somewhere as interface{}?

    If yes then in your range rows you can try to cast it to type []interface{}. So your code should look something like this.

    for _, row := range rows {
        if val, ok := row.Key.([]interface{}); ok {
            fmt.Println(val[1])
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分