代码之家  ›  专栏  ›  技术社区  ›  Bob van Luijt

如何迭代golang中的[]接口{}?

  •  0
  • Bob van Luijt  · 技术社区  · 6 年前

    我在努力争取 键和值 ,这是json封送 Execute 如所示 this example :

    [
        [
            {
                "id": 36,
                "label": "TestThing",
                "properties": {
                    "schema__testBoolean": [
                        {
                            "id": 40,
                            "value": true
                        }
                    ],
                    "schema__testInt": [
                        {
                            "id": 39,
                            "value": 1
                        }
                    ],
                    "schema__testNumber": [
                        {
                            "id": 38,
                            "value": 1.0879834
                        }
                    ],
                    "schema__testString": [
                        {
                            "id": 37,
                            "value": "foobar"
                        }
                    ],
                    "uuid": [
                        {
                            "id": 41,
                            "value": "7f14bf92-341f-408b-be00-5a0a430852ee"
                        }
                    ]
                },
                "type": "vertex"
            }
        ]
    ]
    

    reflect.TypeOf(result) 结果: []interface{} .

    我用这个在数组上循环:

    s := reflect.ValueOf(result)
    for i := 0; i < s.Len(); i++ {
      singleVertex := s.Index(i).Elem() // What to do here?
    }
    

    但我总是犯一些错误,比如:

    reflect.value.interface:无法返回从未报告的 领域或方法

    2 回复  |  直到 6 年前
        1
  •  4
  •   Jonathan Hall    6 年前

    如果您知道这是您的数据结构,那么根本没有理由使用反射。只需使用类型断言:

    for key, value := range result.([]interface{})[0].([]interface{})[0].(map[string]interface{}) {
        // key == id, label, properties, etc
    }
    
        2
  •  1
  •   Himanshu    6 年前

    要获取接口的基础值,请使用类型断言。阅读更多有关 Type assertion 以及它的工作原理。

    package main
    
    import (
        "fmt"
    )
    
    func main() {
         res, err := g.Execute( // Sends a query to Gremlin Server with bindings
               "g.V(x)",
                map[string]string{"x": "1234"},
                map[string]string{},
         )
         if err != nil {
              fmt.Println(err)
              return
         }
         fetchValue(res)
    }
    
    func fetchValue(value interface{}) {
        switch value.(type) {
        case string:
            fmt.Printf("%v is an interface \n ", value)
        case bool:
            fmt.Printf("%v is bool \n ", value)
        case float64:
            fmt.Printf("%v is float64 \n ", value)
        case []interface{}:
            fmt.Printf("%v is a slice of interface \n ", value)
            for _, v := range value.([]interface{}) { // use type assertion to loop over []interface{}
                fetchValue(v)
            }
        case map[string]interface{}:
            fmt.Printf("%v is a map \n ", value)
            for _, v := range value.(map[string]interface{}) { // use type assertion to loop over map[string]interface{}
                fetchValue(v)
            }
        default:
            fmt.Printf("%v is unknown \n ", value)
        }
    }
    
    推荐文章