GoLang 中如何生成 UUID 唯一標識
文章來自:https://www.jb51.net/article/211687.htm
最近在學習 Golang,所以將學習過程記爲筆記,以後翻閱的時候也方便,順便也給大家做一點分享,希望能堅持下去。
現在就開始你的 Go 語言學習之旅吧!人生苦短,let’s Go.
1、什麼是 UUID
通用唯一識別碼(英語:Universally Unique Identifier,簡稱 UUID)是一種軟件建構的標準,亦爲自由軟件基金會組織在分散式計算環境領域的一部份。
UUID 的目的,是讓分散式系統中的所有元素,都能有唯一的辨識信息,而不需要通過中央控制端來做辨識信息的指定。
如此一來,每個人都可以創建不與其它人衝突的 UUID。
在這樣的情況下,就不需考慮數據庫創建時的名稱重複問題。目前最廣泛應用的 UUID,是微軟公司的全局唯一標識符(GUID),而其他重要的應用,則有 Linux ext2/ext3 文件系統、LUKS 加密分區、GNOME、KDE、Mac OS X 等等。
2、Go 中現有的 UUID 第三方生成辦法
目前,golang 中的 uuid 還沒有納入標準庫,我們使用 github 上的開源庫:
go get -u github.com/satori/go.uuid
使用:
package main
import (
"fmt"
"github.com/satori/go.uuid"
)
func main() {
id := uuid.NewV4()
ids := id.String()
}
3、自定義的 UUIDGenerator 實現
功能:
UUIDGenerator 可以生成全局唯一的字符串形式的 ID,ID 由兩部分構成,一部分是用戶指定的前綴,另一部分是數字,數字的最大值爲 4294967295;
UUIDGenerator 可以生成全局唯一的無符號整形數字,數字範圍 0- 4294967295
代碼
package utils
import "fmt"
const (
MAXUINT32 = 4294967295
DEFAULT_UUID_CNT_CACHE = 512
)
type UUIDGenerator struct {
Prefix string
idGen uint32
internalChan chan uint32
}
func NewUUIDGenerator(prefix string) *UUIDGenerator {
gen := &UUIDGenerator{
Prefix: prefix,
idGen: 0,
internalChan: make(chan uint32, DEFAULT_UUID_CNT_CACHE),
}
gen.startGen()
return gen
}
//開啓 goroutine, 把生成的數字形式的UUID放入緩衝管道
func (this *UUIDGenerator) startGen() {
go func() {
for {
if this.idGen == MAXUINT32 {
this.idGen = 1
} else {
this.idGen += 1
}
this.internalChan <- this.idGen
}
}()
}
//獲取帶前綴的字符串形式的UUID
func (this *UUIDGenerator) Get() string {
idgen := <-this.internalChan
return fmt.Sprintf("%s%d", this.Prefix, idgen)
}
//獲取uint32形式的UUID
func (this *UUIDGenerator) GetUint32() uint32 {
return <-this.internalChan
}
測試用例:
package utils
import (
"testing"
"fmt"
)
func TestUUIDGenerator(t *testing.T) {
//新建UUIDGennerator
UUIDFactory := NewUUIDGenerator("idtest")
//獲取UUID
for i:= 0; i < 50; i++{
fmt.Println(UUIDFactory.Get())
}
//獲取uint32 形式的UUID
for i := 0; i < 50; i++{
fmt.Println(UUIDFactory.GetUint32())
}
}
結果
idtest1
idtest2
idtest3
idtest4
idtest5
6
7
8
9
10
PASS
補充:golang 的 UUID 使用
安裝
go get github.com/satori/go.uuid
使用
幾種 UUID 的產生方式:
Version 1, based on timestamp and MAC address (RFC 4122)
Version 2, based on timestamp, MAC address and POSIX UID/GID (DCE 1.1)
Version 3, based on MD5 hashing (RFC 4122)
Version 4, based on random numbers (RFC 4122)
Version 5, based on SHA-1 hashing (RFC 4122)
示例代碼
package main
import (
"fmt"
"github.com/satori/go.uuid"
)
func main() {
// Creating UUID Version 4
// panic on error
u1 := uuid.Must(uuid.NewV4())
fmt.Printf("UUIDv4: %s\n", u1)
// or error handling
u2, err := uuid.NewV4()
if err != nil {
fmt.Printf("Something went wrong: %s", err)
return
}
fmt.Printf("UUIDv4: %s\n", u2)
// Parsing UUID from string input
u2, err := uuid.FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
if err != nil {
fmt.Printf("Something went wrong: %s", err)
}
fmt.Printf("Successfully parsed: %s", u2)
}
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s?__biz=MzU2MDA0Mzk0OQ==&mid=2247483786&idx=1&sn=b2c27da47c2148d0e4028431f90e9c17&chksm=fc0f4348cb78ca5ed07fa56ba9e7fae688bb750f36a0ecb8f2e7ccd0b154c0b987ac91c989b6&scene=21#wechat_redirect