The Go Postgres library defines a type UUID
as such :
type UUID struct {
UUID uuid.UUID
Status pgtype.Status
}
func (dst *UUID) Set(src interface{}) error {
<Remainder Omitted>
My code uses this library:
import pgtype/uuid
string_uuid := uuid.New().String()
fmt.Println("string_uuid = ", string_uuid)
myUUID := pgtype.UUID{}
err = myUUID.Set(string_uuid)
if err != nil {
panic()
}
fmt.Println("myUUID.Bytes = ", myUUID.Bytes)
fmt.Println("string(myUUID.Bytes[:]) = ", string(myUUID.Bytes[:]))
Here is the output:
string_uuid = abadf98f-4206-4fb0-ab91-e77f4380e4e0
myUUID.Bytes = [171 173 249 143 66 6 79 176 171 145 231 127 67 128 228 224]
string(myUUID.Bytes[:]) = ����BO����C���
How can I get back to the original human-readable UUID string abadf98f-4206-4fb0-ab91-e77f4380e4e0
once it is put into myUUID
which is of type pgtype.UUID{}
?
The code in the question uses the pgtype.UUID , not the gofrs UUID linked from question's prose.
The pgtype.UUID type does not have a method to get the UUID string representation, but it's easy enough to do that in application code:
s := fmt.Sprintf("%x-%x-%x-%x-%x", myUUID.Bytes[0:4], myUUID.Bytes[4:6], myUUID.Bytes[6:8], myUUID.Bytes[8:10], myUUID.Bytes[10:16])
Do this if you want hex without the dashes:
s := fmt.Sprintf("%x", myUUID.Bytes)
If the application uses the gofrs UUID , then use:
s := myUUID.UUID.String()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.