drjun1994 2014-01-12 01:59
浏览 177
已采纳

如何在接口上调用len()?

I'm writing a test that a JSON list is empty.

{"matches": []}

The object has type map[string]interface{}, and I want to test that the list is empty.

var matches := response["matches"]
if len(matches) != 0 {
    t.Errorf("Non-empty match list!")
}

However I'm told at compile time that this is invalid

invalid argument matches (type interface {}) for len

If I try casting to a list type:

matches := response["matches"].([]string)

I get a panic:

panic: interface conversion: interface is []interface {}, not []string [recovered]

What do I want to write here?

  • 写回答

1条回答 默认 最新

  • dongzhentiao2326 2014-01-12 03:10
    关注

    JSON parsing with maps in Go uses interfaces everywhere. Imagine you have the following JSON object:

    {
        "stuff" : [
            "stuff1",
            "stuff2",
            "stuff3",
        ]
    }
    

    The Go JSON library will parse the outer object as a map from keys to values, as you've seen in your code. It maps variable names as keys to the values that correspond to those variable names. However, since it has no way of knowing ahead of time what those values are, the value type of the map is simply interface{}. So let's say you know there's a key called "stuff", and you know that its value is an array. You could do:

    arr := myMap["stuff"]
    

    And you know that it's an array type, so you can actually instead do:

    arr := myMap["stuff"].([]interface{})
    

    the problem here is that while you're right that it's an array, and the JSON library knows this, it has no way of knowing that every element will be of type string, so there's no way for it to decide that the array type should actually be []string. Imagine if you had done this instead:

    {
        "stuff" : [
            "stuff1",
            "stuff2",
            3
        ]
    }
    

    Well "stuff" can't now be an array of strings because one of the elements isn't a string. In fact, it can't be an array of anything - there's no single type that would satisfy the types of all of the elements. So that's why the Go JSON library has no choice but to leave it as []interface{}. Luckily, since all you want is the length, you're already done. You can just do:

    arr := myMap["stuff"].([]interface{})
    l := len(arr)
    

    Now that's all fine and good, but let's say that down the road you want to actually look at one of the elements. You could now take out an element and, knowing that it's a string, do:

    arr := myMap["stuff"].([]interface{})
    iv := arr[0] // interface value
    sv := iv.(string) // string value
    

    NOTE

    When I say "array," I mean array in the JSON sense - these are JSON arrays. The data structure that represents them in Go is called a "slice" (Go has arrays too, but they're a separate thing - if you're used to arrays in languages like C or Java, Go slices are the closest analogue).

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么