FAQ
Hello,

I tried to call c.Insert once for each of c variables with below code, but
I got the following errors:
./chemeo.go:11: cannot convert entry (type []string) to type []byte
./chemeo.go:51: cannot use chemicals (type map[string]interface {}) as type
*map[string]interface {} in function argument".

package main

import (
"encoding/json"
"fmt"
"labix.org/v2/mgo"
"labix.org/v2/mgo/bson"
)

func insertEntry(j *map[string]interface{}, entry []string) {
err := json.Unmarshal([]byte(entry), &j)
if err != nil {
panic(err)
}

}

func main() {
c1 := `{
"mw" : 42.0922,
"ΔfH°gas" : {
"value" : 372.38,
"units" : "kJ/mol"
},
"S°gas" : {
"value" : 216.81,
"units" : "J/mol×K"
},
"index" : [
{"name" : "mw", "value" : 42.0922},
{"name" : "ΔfH°gas", "value" : 372.38},
{"name" : "S°gas", "value" : 216.81}
]
}`

c2 := `{
"name": "silicon",
"mw": 32.1173,
"index": [
{
"name": "mw",
"value": 32.1173
}
]
}`

var chemicals_entries []string
chemicals_entries = append(chemicals_entries, c1, c2)
chemicals := make(map[string]interface{})

insertEntry(chemicals, chemicals_entries)

chemical := chemicals["ΔfH°gas"].(map[string]interface{})
fmt.Println("value: ", chemical["value"].(float64))
fmt.Println("units: ", chemical["units"].(string))

session, err := mgo.Dial("localhost")

if err != nil {
panic(err)
}
defer session.Close()

// Optional. Switch the session to a monotonic behavior.
session.SetMode(mgo.Monotonic, true)

c := session.DB("test").C("chemicals")
err = c.Insert(&chemicals)
if err != nil {
panic(err)
}

err = c.Find(bson.M{"name": "silicon"}).One(&chemicals)
if err != nil {
panic(err)
}

fmt.Println(chemicals)
fmt.Println("mw:", chemicals["mw"])

fmt.Println("value: ", chemical["value"].(float64))
fmt.Println("units: ", chemical["units"].(string))

}

How is it possible to fix it?

Thank you in advance.

Mic

--
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

  • Xing Xing at Mar 19, 2013 at 1:32 am
    于 Mon, 18 Mar 2013 17:23:31 -0700 (PDT)
    Mic L <mictadlo@gmail.com> 写道:

    err := json.Unmarshal([]byte(entry), &j)
    []byte means byte array (a byte slice). A byte array equals to a
    string. So, you can't convert a []string (a string array) into a []byte
    (or a string).

    func insertEntry(j *map[string]interface{}, entry []string)
    This function need a pointer of map as the first param, but a map var
    was passed in.

    try:

    func insertEntry(j map[string]interface{}, entry []string)

    or

    m := make(map[string]interface{})
    insertEntry(&m, entry)

    PS: Why don't you use play.golang.org for the codes?

    --
    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.
  • Mic L at Mar 19, 2013 at 6:12 am
    Thank you for your answer, but unfortunately I do not understand how to
    insert the two JSON entries (c1 and c2) from this code
    http://play.golang.org/p/nH4KRrJqhz in order to copy the Java example
    below from MongoDB book (The definitive guide) page 157:

    public static DBObject createSiliconNitride() {
    BasicDBObject sn = new BasicDBObject();
    sn.put("name", "silicon nitride");
    sn.put("mw", 42.0922);
    BasicDBObject deltafHgas = new BasicDBObject();
    deltafHgas.put("value", 372.38);
    deltafHgas.put("units", "kJ/mol");
    sn.put("ΔfH°gas", deltafHgas);
    BasicDBObject sgas = new BasicDBObject();
    sgas.put("value", 216.81);
    sgas.put("units", "J/mol×K");
    sn.put("S°gas", sgas);
    ArrayList<BasicDBObject> index = new ArrayList<BasicDBObject>();
    index.add(BasicDBObjectBuilder.start()
    .add("name", "mw").add("value", 42.0922).get());
    index.add(BasicDBObjectBuilder.start()
    .add("name", "ΔfH°gas").add("value", 372.38).get());
    index.add(BasicDBObjectBuilder.start()
    .add("name", "S°gas").add("value", 216.81).get());
    sn.put("index", index);
    return sn;
    }

    The updated GO code can be found here http://play.golang.org/p/nH4KRrJqhz .

    How to insert the two JSON entries (c1 and c2) in order to get same results
    how the above Java code.

    Thank you in advance.

    Mic


    On Tuesday, March 19, 2013 11:31:50 AM UTC+10, mikespook wrote:

    于 Mon, 18 Mar 2013 17:23:31 -0700 (PDT)
    Mic L <mict...@gmail.com <javascript:>> 写道:

    err := json.Unmarshal([]byte(entry), &j)
    []byte means byte array (a byte slice). A byte array equals to a
    string. So, you can't convert a []string (a string array) into a []byte
    (or a string).

    func insertEntry(j *map[string]interface{}, entry []string)
    This function need a pointer of map as the first param, but a map var
    was passed in.

    try:

    func insertEntry(j map[string]interface{}, entry []string)

    or

    m := make(map[string]interface{})
    insertEntry(&m, entry)

    PS: Why don't you use play.golang.org for the codes?
    --
    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.
  • Clbanning at Mar 19, 2013 at 9:51 am
    http://play.golang.org/p/03uzUuuE97
    On Monday, March 18, 2013 7:23:31 PM UTC-5, Mic L wrote:

    Hello,

    I tried to call c.Insert once for each of c variables with below code, but
    I got the following errors:
    ./chemeo.go:11: cannot convert entry (type []string) to type []byte
    ./chemeo.go:51: cannot use chemicals (type map[string]interface {}) as
    type *map[string]interface {} in function argument".

    package main

    import (
    "encoding/json"
    "fmt"
    "labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"
    )

    func insertEntry(j *map[string]interface{}, entry []string) {
    err := json.Unmarshal([]byte(entry), &j)
    if err != nil {
    panic(err)
    }

    }

    func main() {
    c1 := `{
    "mw" : 42.0922,
    "ΔfH°gas" : {
    "value" : 372.38,
    "units" : "kJ/mol"
    },
    "S°gas" : {
    "value" : 216.81,
    "units" : "J/mol×K"
    },
    "index" : [
    {"name" : "mw", "value" : 42.0922},
    {"name" : "ΔfH°gas", "value" : 372.38},
    {"name" : "S°gas", "value" : 216.81}
    ]
    }`

    c2 := `{
    "name": "silicon",
    "mw": 32.1173,
    "index": [
    {
    "name": "mw",
    "value": 32.1173
    }
    ]
    }`

    var chemicals_entries []string
    chemicals_entries = append(chemicals_entries, c1, c2)
    chemicals := make(map[string]interface{})

    insertEntry(chemicals, chemicals_entries)

    chemical := chemicals["ΔfH°gas"].(map[string]interface{})
    fmt.Println("value: ", chemical["value"].(float64))
    fmt.Println("units: ", chemical["units"].(string))

    session, err := mgo.Dial("localhost")

    if err != nil {
    panic(err)
    }
    defer session.Close()

    // Optional. Switch the session to a monotonic behavior.
    session.SetMode(mgo.Monotonic, true)

    c := session.DB("test").C("chemicals")
    err = c.Insert(&chemicals)
    if err != nil {
    panic(err)
    }

    err = c.Find(bson.M{"name": "silicon"}).One(&chemicals)
    if err != nil {
    panic(err)
    }

    fmt.Println(chemicals)
    fmt.Println("mw:", chemicals["mw"])

    fmt.Println("value: ", chemical["value"].(float64))
    fmt.Println("units: ", chemical["units"].(string))

    }

    How is it possible to fix it?

    Thank you in advance.

    Mic
    --
    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.
  • Mic L at Mar 20, 2013 at 12:53 am
    Thank you, but now c2 is overriding c1 (
    http://play.golang.org/p/TSDrxhfCV4 ).

    What would I have to change to avoid it?

    Thank you in advance.

    Mic
    On Tuesday, March 19, 2013 7:51:20 PM UTC+10, clba...@gmail.com wrote:

    http://play.golang.org/p/03uzUuuE97
    On Monday, March 18, 2013 7:23:31 PM UTC-5, Mic L wrote:

    Hello,

    I tried to call c.Insert once for each of c variables with below code,
    but I got the following errors:
    ./chemeo.go:11: cannot convert entry (type []string) to type []byte
    ./chemeo.go:51: cannot use chemicals (type map[string]interface {}) as
    type *map[string]interface {} in function argument".

    package main

    import (
    "encoding/json"
    "fmt"
    "labix.org/v2/mgo"
    "labix.org/v2/mgo/bson"
    )

    func insertEntry(j *map[string]interface{}, entry []string) {
    err := json.Unmarshal([]byte(entry), &j)
    if err != nil {
    panic(err)
    }

    }

    func main() {
    c1 := `{
    "mw" : 42.0922,
    "ΔfH°gas" : {
    "value" : 372.38,
    "units" : "kJ/mol"
    },
    "S°gas" : {
    "value" : 216.81,
    "units" : "J/mol×K"
    },
    "index" : [
    {"name" : "mw", "value" : 42.0922},
    {"name" : "ΔfH°gas", "value" : 372.38},
    {"name" : "S°gas", "value" : 216.81}
    ]
    }`

    c2 := `{
    "name": "silicon",
    "mw": 32.1173,
    "index": [
    {
    "name": "mw",
    "value": 32.1173
    }
    ]
    }`

    var chemicals_entries []string
    chemicals_entries = append(chemicals_entries, c1, c2)
    chemicals := make(map[string]interface{})

    insertEntry(chemicals, chemicals_entries)

    chemical := chemicals["ΔfH°gas"].(map[string]interface{})
    fmt.Println("value: ", chemical["value"].(float64))
    fmt.Println("units: ", chemical["units"].(string))

    session, err := mgo.Dial("localhost")

    if err != nil {
    panic(err)
    }
    defer session.Close()

    // Optional. Switch the session to a monotonic behavior.
    session.SetMode(mgo.Monotonic, true)

    c := session.DB("test").C("chemicals")
    err = c.Insert(&chemicals)
    if err != nil {
    panic(err)
    }

    err = c.Find(bson.M{"name": "silicon"}).One(&chemicals)
    if err != nil {
    panic(err)
    }

    fmt.Println(chemicals)
    fmt.Println("mw:", chemicals["mw"])

    fmt.Println("value: ", chemical["value"].(float64))
    fmt.Println("units: ", chemical["units"].(string))

    }

    How is it possible to fix it?

    Thank you in advance.

    Mic
    --
    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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 19, '13 at 1:03a
activeMar 20, '13 at 12:53a
posts5
users3
websitegolang.org

3 users in discussion

Mic L: 3 posts Xing Xing: 1 post Clbanning: 1 post

People

Translate

site design / logo © 2021 Grokbase