For my restful api I want to implement shorter urls based on url-safe base42-encoded UUID's version 4 (In future I'll use MongoDB's internal one's instead).
The generation works fine, but the base64 library of Go doesn't seem to encode the UUID as string as expected. The outpout is 48 characters long instead of 22 (as shown here in Python).
Here is my code:
package main
import (
"encoding/base64"
"fmt"
"github.com/nu7hatch/gouuid"
)
func printRandomUUID() {
uid, _ := uuid.NewV4()
uid64 := base64.URLEncoding.EncodeToString([]byte(uid.String()))
fmt.Println(uid64, len(uid64))
}
func main() {
for i := 0; i < 5; i++ {
printRandomUUID()
}
}
And here is a possible output:
OGJhNzdhODItNjc5Yi00OWIyLTYwOGEtODZkYjA2Mzk0MDJj 48
YzE3YTNmY2EtOWM1MC00ZjE2LTQ3YTAtZGI3ZGQyNGI4N2Fj 48
ODFmZDU3ZDgtNjA2Ni00ZjYwLTUyMDUtOTU0MDVhYzNjZTNh 48
NjhkNTY3ZDAtOGE1Yy00ZGY2LTVmZmMtZTg2ZDEwOTlmZjU3 48
MzhhZmU0MDctMDE3Ny00YjhjLTYyYzctYWYwMWNlMDkwOWRh 48
As shown is the output not shorter but longer! Did I implemented the encoding the wrong way?