我有一個結構片。如何搜索golang片中的元素
type Config struct {
Key string
Value string
}
// I form a slice of the above struct
var myconfig []Config
// unmarshal a response body into the above slice
if err := json.Unmarshal(respbody, &myconfig); err != nil {
panic(err)
}
fmt.Println(config)
Here is the output of this
[{key1 test} {web/key1 test2}]
我該如何搜索這個數組來獲取元素key="key1"
?
作爲您的配置結構看起來像一個簡單的地圖我要指出的是,你可以在任何JSON數據解碼爲'地圖[字符串]接口{}'。如果你有興趣,結賬[這個官方博客文章](https://blog.golang.org/json-and-go) – tsabsch