type Person struct { Name string `bson:"name"` Age int `bson:"age"` } data := bson.Raw{0x15, 0x00, 0x00, 0x00, 0x02, 0x6e, 0x61, 0x6d, 0x65, 0x00, 0x05, 0x00, 0x00, 0x00, 0x62, 0x6f, 0x62, 0x00, 0x00, 0x03, 0x61, 0x67, 0x65, 0x00, 0x1e, 0x00, 0x00, 0x00} var person Person err := data.Unmarshal(&person) if err != nil { log.Fatal(err) } fmt.Println(person.Name, person.Age) // Output: bob 30
data := bson.Raw{0x13, 0x00, 0x00, 0x00, 0x04, 0x6b, 0x65, 0x79, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f} var m map[string]interface{} err := data.Unmarshal(&m) if err != nil { log.Fatal(err) } fmt.Println(m) // Output: map[key:Hello]In the above example, we create a `bson.Raw` object with BSON data representing a map with one key "key" and value "Hello". We then use the `Unmarshal` method to decode the raw BSON bytes into a `map[string]interface{}` variable `m`. Overall, the `bson.Raw.Unmarshal` method is a useful way to decode raw BSON bytes into Go types.