How to convert bason to json(with src)?

76 zobrazení
Přeskočit na první nepřečtenou zprávu

Alex Luya

nepřečteno,
13. 8. 2012 22:34:2213.08.12
komu: mgo

What I really want to do is hide mongdb connections stuffs(creation,clone..close) and error handling stuff like this:

func ExcuteQuery(collection string, query interface{}) []byte {
ses := ensureMongoConnection("ExcuteQuery")
defer ses.Close()

var result []interface{}
err := ses.DB(databaseName).C(collection).Find(query).All(&result)
if err != nil {
  panic(err)
}

// check err
rawjson, err := json.Marshal(result)
// return result
if err != nil {
  panic(err)
}
return rawjson
}

And test caller will like this:

func (s *MySuite) TestGetModel(c *C) {
res := mgd.ExcuteQuery(collection, nil)
//************************************here something get printed out***********************************
fmt.Println(res)
var list []MySuite
json.Unmarshal(res, list)
//************************************but why here is length of list 0?***********************************
c.Check(list[0].Value, Equals, value)
}

Gustavo Niemeyer

nepřečteno,
14. 8. 2012 6:40:0614.08.12
komu: mgo-...@googlegroups.com
On Mon, Aug 13, 2012 at 11:34 PM, Alex Luya <alexand...@gmail.com> wrote:
> What I really want to do is hide mongdb connections
> stuffs(creation,clone..close) and error handling stuff like this:

Alex, please use a pastebin in the future, such as gist.github.com,
rather than pasting a lot of code inline.

Also, sample files should be self contained so we can run them, since
otherwise we'll have to do guess work just to get your stuff running.

> json.Unmarshal(res, list)

This should be &list. Check the error result.


gustavo @ http://niemeyer.net
Odpovědět všem
Odpověď autorovi
Přeposlat
0 nových zpráv