When I try to marshal []byte to JSON format, I only got a strange string.
Please look the following code.
I have two doubt:
How can I marshal []byte to JSON?
Why []byte become this string?
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
type ColorGroup struct {
ByteSlice []byte
SingleByte byte
IntSlice []int
}
group := ColorGroup{
ByteSlice: []byte{0,0,0,1,2,3},
SingleByte: 10,
IntSlice: []int{0,0,0,1,2,3},
}
b, err := json.Marshal(group)
if err != nil {
fmt.Println("error:", err)
}
os.Stdout.Write(b)
}
the output is:
{"ByteSlice":"AAAAAQID","SingleByte":10,"IntSlice":[0,0,0,1,2,3]}
golang playground: https://play.golang.org/p/wanppBGzNR