GoLang中生成UUID唯一标识的实现
作者:私念 时间:2024-05-22 10:19:54
1、什么是UUID
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)
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持asp之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/tiancityycf/article/details/102698974
标签:GoLang,UUID,唯一标识
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
CentOS7 LNMP+phpmyadmin环境搭建 第三篇phpmyadmin安装
2023-10-17 03:23:18
![](https://img.aspxhome.com/file/2023/0/75060_0s.png)
jupyter notebook 自定义python解释器的过程详解
2021-03-07 15:31:36
![](https://img.aspxhome.com/file/2023/7/131107_0s.jpg)
jupyter notebook如何使用matlab
2022-11-08 18:17:51
![](https://img.aspxhome.com/file/2023/8/107028_0s.png)
列表模块是否需要标题
2009-06-25 14:11:00
![](https://img.aspxhome.com/file/UploadPic/20096/25/g2009620101624-60s.jpg)
sql获取存储过程返回数据过程解析
2024-01-20 07:52:47
python 监控logcat关键字功能
2022-06-13 13:36:17
![](https://img.aspxhome.com/file/2023/5/126905_0s.jpg)
Python入门学习之字符串与比较运算符
2023-10-14 22:09:40
![](https://img.aspxhome.com/file/2023/0/75290_0s.jpg)
python中用Scrapy实现定时爬虫的实例讲解
2023-07-26 05:47:01
Python数组变形的几种实现方法
2021-08-20 09:30:47
![](https://img.aspxhome.com/file/2023/7/85387_0s.png)
Python 读取某个目录下所有的文件实例
2022-08-27 20:04:13
win7安装python生成随机数代码分享
2021-05-18 10:47:39
Python之多进程与多线程的使用
2021-01-10 15:03:57
python委派生成器的具体方法
2022-06-14 01:23:39
![](https://img.aspxhome.com/file/2023/8/120988_0s.png)
用python打包exe应用程序及PyInstaller安装方式
2023-09-20 03:55:21
![](https://img.aspxhome.com/file/2023/8/133878_0s.png)
Python Django给admin添加Action的方法实例详解
2023-10-30 13:11:35
![](https://img.aspxhome.com/file/2023/6/135276_0s.png)
区别JavaScript函数声明与变量声明
2024-04-18 09:34:42
Python字符串逆序输出的实例讲解
2023-04-21 22:59:46
GitHub Eclipse配置使用教程详解
2023-11-04 10:23:02
![](https://img.aspxhome.com/file/2023/4/109524_0s.png)
python热力图实现简单方法
2023-10-28 06:14:41
通过分析SQL语句的执行计划优化SQL
2011-10-24 20:03:20