如何从查询结果中检查相同的值并在golang中捕获第一个值

时间:2018-03-28 06:04:11

标签: go

请帮帮我,如何从查询中查看相同的值 并在golang或python中捕获第一个值

{     “数据”:[     “1234567001”,     “1234567001”,     “1234567001”,     “1234567001”,     “4567898001”,     “4567898001”,     “4567898001”     ]   }

如何使用go lang

从数据中获取2个不同的值
vals := []interface{}{}
for _, row := range result {
    nobil := row.Nobilling
    vals = append(vals, nobil)

    if nobil == row.billing {
        continue
    }

    i++
}

我只想存储数组中的不同值

1 个答案:

答案 0 :(得分:1)

我认为你可能正在寻找反思...我说"思考"因为你说问题的方式令人困惑。像这样的东西?

res := []interface{}{
    1,
    "row2",
    struct { str string } { str: "what the heck???" },
}
// one way of reflecting
for i, v := range res {
    if r, ok := v.(int); ok {
        fmt.Printf("%d: You have an int: %d\n", i, r)
    } else if r, ok := v.(string); ok {
        fmt.Printf("%d: You have a string: %q\n", i, r)
    } else {
        fmt.Printf("%d: Have no idea what this type is: %T\n", i, v)
    }
}
// a cleaner way of reflecting (in my opinion)
for i, v := range res {
    switch r := v.(type) {
    case int:
        fmt.Printf("%d: You have an int: %d\n", i, r)
    case string:
        fmt.Printf("%d: You have a string: %q\n", i, r)
    default:
        fmt.Printf("%d: Have no idea what this type is: %T\n", i, v)
    }
} 

在操场上查看:https://play.golang.org/p/ca7umhCBXt3

或者在" Go of Go":https://tour.golang.org/methods/16