One thing I notice in your example is that UnmarshalBSON internally calls bson.Unmarshal(data, &m) rather than bson.Unmarshal(data, &r). The next line tries to print out the contents of r, but it’s only been set to the zero-value in the variable declaration, so it will be an empty string.
Can you provide some example input/output to show your desired data format? We can help you write Marshal/Unmarshal implementations to do that.
Thanks for the example output. This is a little more complex in the driver, but you can use the ValueMarshaler and ValueUnmarshaler interfaces to do this. I’ve written up some example code at Go Playground - The Go Programming Language.
If you’re doing an mgo to driver migration, you might be interested in looking at the mgo-compatible BSON registry we’ve written, which offers support for interfaces similar to mgo’s Getter and Setter. See mgocompat package - go.mongodb.org/mongo-driver/bson/mgocompat - Go Packages for some more information about this BSON registry. If you want to try using it, you can add this line to your mongo.Client construction to enable it everywhere:
// You can also use mgocompat.RegistryRespectNilValues for a registry
// that's compatible with mgo's RespectNilValues behavior
clientOpts := options.Client().SetRegistry(mgocompat.Registry)
client, err := mongo.Connect(ctx, clientOpts)