Encode ulids as protobuf directly.
-
Import the proto file in your proto code
mymessage.proto
:syntax = "proto3"; package mypackage; import "ulid.proto"; option go_package = "example.org/mypackage"; message MyMessage { ulid.ULID id = 1; }
-
Add this library to your code:
$ go get github.com/exaring/ulid-protobuf@latest
-
Then compile your proto files with
protoc
, e.g.:$ protoc --proto_path=$(go list -m -f '{{.Dir}}' github.com/exaring/ulid-protobuf) \ --go_out=paths=source_relative:. \ --proto_path=. ./mymessage.proto
Note the trick with
--proto_path
: it allowsprotoc
to find theulid.proto
file inside the go module's source. -
Use ULIDs in you code:
// send ULIDs as protobuf msg := &MyMessage{ Id: ulidpb.FromULID(ulid.Make()), } // receive ULIDs from protobuf u := msg.Id.AsULID()