Skip to content

how to map cbor tag to golang basic type? #599

Closed Answered by fxamacker
agufagit asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @agufagit,

is there a way to map cbor tag directly to golang basic type string without defining a new type?

We intentionally don't provide a direct mapping between CBOR tags and Go's basic types. We need to define new Go types to map to CBOR tags.

This approach prevents mistakes and other issues because a Go string type can be used with different CBOR tags. For example, CBOR tags defined in RFC8949 1, 32, 33, 34, and 36 all use data item of CBOR text string type.

BTW, if you just need out-of-box support for NewString with a CBOR tag, you don't need to implement NewString.Unmarshal. For example, https://go.dev/play/p/CTcLik_KSB9

// Example for disussion and issue at:
// - https://gith…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by fxamacker
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants