I'm trying to get a JSON string pulled from a document and into SimpleJson in GOlang, though I've run into a problem with the types (again..)
I get the following error:
cannot use document[0] (type uint8) as type []byte in function argument
and the line which bugs up is:
js, err := simplejson.NewJson(document[0])
Could anyone please help me fix this, and also is there a good place I can read up about types and conversions? Having come from php which has no types and python where the conversions are simple, GO is a bit confusing on this front.
Thanks :-)
First of all, uint8
is simply an alias for byte
. Therefor []uint8
is the same as []byte
.
What the error message tells you is that document[0] is of type uint8
and not []byte
From your comment it is clear that document
is a string
. You should just convert it to []byte. If the JSON data in itself contains an array, you must first parse the document (pass it to NewJson) before you can retrieve the first value of the JSON array.
So, instead change your code to:
js, err := simplejson.NewJson([]byte(document))
// error testing
jsonArray, err := js.Array()
// more error testing
fmt.Println(jsonArray[0])
(I assume you have imported the github.com/xiocode/toolkit/simplejson
package. The only one I could find with simplejson.NewJson)