Using build in UUID function from the driver in golang mongodb

I notice that we have

package uuid // import “

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

go: finding module for package
go: downloading v1.11.1 imports module found (v1.11.1), but does not contain package

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.