Using build in UUID function from the driver in golang mongodb

I notice that we have

package uuid // import “go.mongodb.org/mongo-driver/x/mongo/driver/uuid

But I’m not use whether this can be import from our app?

go: finding module for package go.mongodb.org/mongo-driver/x/mongo/driver/uuid
go: downloading go.mongodb.org/mongo-driver v1.11.1
accelbyte.net/customer-shared-justice-inventory-service/pkg/inventory/domain/models imports
go.mongodb.org/mongo-driver/x/mongo/driver/uuid: module go.mongodb.org/mongo-driver@latest found (v1.11.1), but does not contain package go.mongodb.org/mongo-driver/x/mongo/driver/uuid

Hey @Altiano_Gerung thanks for the question! The x/mongo/driver/uuid package was moved to an internal/ directory in the Go Driver v1.10.0 release and is not intended for use outside of the Go Driver. You should use another UUID library instead, like google/uuid or gofrs/uuid.

But it would be stored as string instead of UUID type wouldn’t it? I was thinking of using the same type as you have when using UUID(),

it will result in better storage efficiency and possibly better performance, am I right?

The underlying type for the internal UUID type in the Go driver and the UUID types in the google/uuid and gofrs/uuid libraries are all [16]byte, so the BSON marshaling behavior should be the same for all 3 (they are marshaled as the BSON “binary” data type). There is currently no UUID type provided by the Go driver or Go BSON library that has any special marshaling/unmarshaling behavior.

Check out an example of the default marshaling behavior on the Go Playground here.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.