FAQ
Hi list.

I wrote small library to select JSON element from interface{}.
Go require struct for scaning data from JSON. As you know, it's possible
with interface{}, but it's harder to select deepy structure like:

var a interface{}
json.NewDecoder(os.Stdin).Decode(&a)
a.(map[string]interface{})["foo"].(interface[string]interface{})["bar"].([]interface{})[2].(string)

I'm thinking go-scan is useful to select deepy structure in interface{}.
For example:

1. Scan string

    // { "foo": { "bar": "baz" } }
    var s string
    scan.ScanTree(a, "/foo/bar", &s) // s should be "baz"

2. Scan float64

    // { "foo": { "bar": 3 } }
    var f float64
    scan.ScanTree(a, "/foo/bar", &f) // s should be 3.0

3. Scan bool

    // { "foo": { "bar": true } }
    var b bool
    scan.ScanTree(a, "/foo/bar", &b) // s should be true

4. Scan slice of string

    // { "foo": { "bar": ["go", "lang"] } }
    var s []string
    scan.ScanTree(a, "/foo/bar", &s) // s[0] should be "go", s[1] should be
"lang"

https://github.com/mattn/go-scan

Thanks.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 21, '13 at 12:39p
activeMay 21, '13 at 12:39p
posts1
users1
websitegolang.org

1 user in discussion

Mattn: 1 post

People

Translate

site design / logo © 2022 Grokbase