I’ve got a struct with a []uint8 member and I’m writing it with json.Marshal. Trouble is, it’s interpreting the uint8s as chars and it outputs a string rather than an array of numbers.
I can get this to work if it’s a []int, but I don’t want to have to allocate and copy over the items if I can avoid it. Can I?
According to the docs, a
[]bytewill be encoded as a Base64 string.So I think that you may need to make your struct implement the Marshaler interface by implementing your own
MarshalJSONmethod that makes a more desirable JSON array encoding out of your[]uint8.Take this example:
http://play.golang.org/p/Tip59Z9gqs
Or maybe a better idea would be to make a new type that has
[]uint8as its underlying type, make that type aMarshaler, and use that type in your struct.http://play.golang.org/p/6aURXw8P5d