Shortening uuid as string

Hi everyone!
I am looking for an alternative to make the UUID.String() shorter.

The current length is 36, for example: 964238a0-8188-465f-8347-657819bf789a.
Encoding with ascii85 the length is 20, for example: Q9mR(JVZd&K1BKt)8CEb

Any ideas?

Example in The Go Playground (link):

package main

import (
    "encoding/ascii85"
    "fmt"

    "github.com/google/uuid"
)

func main() {
    originalUUID := uuid.New()
    originalSlice, _ := originalUUID.MarshalBinary()
    enc := make([]byte, ascii85.MaxEncodedLen(len(originalUUID)))
    ascii85.Encode(enc, originalSlice)
    newStringID := originalUUID.String()
    
    fmt.Printf("uuid    - len: %d, string: '%s'\n", len(newStringID), newStringID)
    fmt.Printf("ascii85 - len: %d, string: '%s'\n", len(enc), string(enc))

    // Just for testing:
    dst := make([]byte, 16)
    ascii85.Decode(dst, enc, true)
    uuidDecoded, _ := uuid.FromBytes(dst)
    fmt.Printf("decoded - len: %d, string: '%s'\n", len(uuidDecoded.String()), uuidDecoded.String())
}

Output:

uuid - len: 36, string: ‘964238a0-8188-465f-8347-657819bf789a’
ascii85 - len: 20, string: ‘Q9mR(JVZd&K1BKt)8CEb’
decoded - len: 36, string: ‘964238a0-8188-465f-8347-657819bf789a’

HI @GonzaSaya,

I am not a UUID expert but to me, ascii85 already seems a very dense format, due to the large number of characters used for encoding.

To get an even shorter ID, I would try one of the following approaches.

  1. Use binary compression. This is suitable if the ID does not need to be printable.

  2. Use another Unique ID format. UUIDs are meant to be globally unique. That is, it should be very unlikely that the same UUID is generated twice anywhere in the world. If you are ok with a unique ID that is unique only within your database or server cluster or company etc, there are other algorithms available besides UUID that generate shorter unique IDs.

Why not just store it as a BLOB/BINARY/BYTEA or whatever data type your DBMS stores binary data as? Why convert it to a string in the DB?

I need to send it to a third party in a json as string.

Do yourself a favour and send it to them in the canonical representation.

That will cause you much less problems in the long term.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.