Output UUID in Go as a short string

后端 未结 2 988
囚心锁ツ
囚心锁ツ 2021-01-31 23:19

Is there a built in way, or reasonably standard package that allows you to convert a standard UUID into a short string that would enable shorter URL\'s?

I.e. taking adva

2条回答
  •  梦谈多话
    2021-01-31 23:44

    As suggested here, If you want just a fairly random string to use as slug, better to not bother with UUID at all.

    You can simply use go's native math/rand library to make random strings of desired length:

    import (
    "math/rand"
    "encoding/hex"
    )
    
    
    b := make([]byte, 4) //equals 8 charachters
    rand.Read(b) 
    s := hex.EncodeToString(b)
    

提交回复
热议问题