I want to be able to output raw bson data I am getting from golang's mgo library to the console for debugging purposes, but have been unable to find out how to accomplish this.
With JSON I do it like this:
formatedData, err := json.MarshalIndent(rawData, "", " ")
if err != nil {
log.Print(err)
}
fmt.Printf("%s", formatedData)
Is there an equivalent way to do this with BSON?
bson is a binary format and it is just a slice of bytes. It is in itself not readable by a human as this format holds information about the length of fields, etc. and holds all data very compact. It is already encoded, so there is no need to marshal it.
You can output as it is, but it will be not readable.
See the bson spec here: http://bsonspec.org/#/specification
If you want to see all contents of the bson, you could Unmarshal it into a map:
m := map[string]interface{}{}
rawData.Unmarshal(&m)
fmt.Printf("%+v
", m)