go-nanoid:Go 短 ID 生成庫

"An amazing level of senseless perfectionism, which is simply impossible not to respect."

go-nanoid (github.com/matoous/go-nanoid) 是 JavaScript 知名庫 ai/nanoid (github.com/ai/nanoid) 的 Go 語言實現版本。原版 nanoid 由 Andrey Sitnik 創建,因其小巧、快速和安全的特點在 JavaScript 生態系統中廣受歡迎。

安裝

go get github.com/matoous/go-nanoid/v2

生成默認 ID

id := gonanoid.Must()
// xRKQXFUpgolDvl0g_GfZz

自定義字母表

id = gonanoid.MustGenerate("0123456789", 6)
// 869525

總結

matoous/go-nanoid 庫爲 Go 語言項目提供了生成短小、安全且 URL 友好的唯一 ID 的高效解決方案。它繼承了 Nano ID 源自 JavaScript 的優秀設計,並通過 Go 語言的 crypto/rand 實現了加密級別的安全性。

通過簡單的引入和幾行代碼,就能在項目中獲得更美觀且難以預測的唯一標識,是一個極具吸引力的選擇。


References
https://github.com/matoous/go-nanoid

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/hAMmDnyWFiQ_R2DmRTFBZg