Based on @dom answer (in the comments), there is the workaround I am now using:
package your_package
import "github.com/globalsign/mgo/bson"
func GenerateNewGuidHelper() *bson.ObjectId {
id := bson.NewObjectId()
return &id
}
func IsStringIdValid(id string) bool {
return id != "" && bson.IsObjectIdHex(id)
}
func ConvertStringIdToObjectId(id string) *bson.ObjectId {
if id != "" && bson.IsObjectIdHex(id){
bsonObjectId := bson.ObjectIdHex(id)
return &bsonObjectId
}
return nil
}
func ConvertStringIdsToObjectIds(ids []string) []*bson.ObjectId {
var _ids []*bson.ObjectId
for _, id := range ids {
_ids = append(_ids, ConvertStringIdToObjectId(id))
}
return _ids
}
func IsObjectIdValid(id *bson.ObjectId) bool {
return id.Hex() != "" && bson.IsObjectIdHex(id.Hex())
}
func ConvertObjectIdToStringId(id *bson.ObjectId) string {
if id != nil {
return id.Hex()
}
return ""
}
func ConvertObjectIdsToStringIds(ids []*bson.ObjectId) []string {
var _ids []string
for _, id := range ids {
_ids = append(_ids, ConvertObjectIdToStringId(id))
}
return _ids
}
Also, as said @dom, I am now saving my mongoDB ID as *bson.ObjectId
instead of bson.ObjectId
. Example:
package datamodels
import (
"github.com/globalsign/mgo/bson"
)
type User struct {
ID *bson.ObjectId `protobuf:"bytes,1,opt,name=id,proto3" json:"_id,omitempty" bson:"_id,omitempty"`
}
I hope it helps !