개요
- collection-go
package main
import (
"fmt"
. "github.com/jmnote/collection-go"
)
func main() {
m := map[string]interface{}{
"ids": []interface{}{1, 2, 3},
"fruits": []interface{}{"apple", "banana"},
}
m["ids"] = append(List(m["ids"]), 4)
m["fruits"] = append(List(m["fruits"]), "melon")
fmt.Println(m)
}
package main
import (
"fmt"
. "github.com/jmnote/collection-go"
)
func main() {
jsonBytes := []byte(`{"status":"hello","data":{"a":"world","b":[1,"lorem"]}}`)
// 간편 추출
var s string
s = List(Dict(Collect(jsonBytes)["data"])["b"])[1].(string)
fmt.Println(s) // lorem
// 해설
o := Collect(jsonBytes)
fmt.Println(o) // map[data:map[a:world b:[1 lorem]] status:hello]
fmt.Println(o["data"]) // map[a:world b:[1 lorem]]
fmt.Println(Dict(o["data"])["b"]) // [1 lorem]
fmt.Println(List(Dict(o["data"])["b"])[1]) // lorem
}
package main
import (
"encoding/json"
"fmt"
. "github.com/jmnote/collection-go"
)
type response struct {
Status string `json:"status"`
Data interface{} `json:"data,omitempty"`
}
func main() {
jsonBytes := []byte(`{"status":"hello","data":{"a":"world","b":[1,"lorem"]}}`)
var response response
json.Unmarshal(jsonBytes, &response)
fmt.Println(response)
fmt.Println(Dict(response.Data)["b"])
fmt.Println(List(Dict(response.Data)["b"])[1])
var s string
s = List(Dict(response.Data)["b"])[1].(string)
fmt.Println(s)
}
같이 보기