commit 4741d211f26fdbfdbdd0954cfe2aa2b1b8f8724b Author: 李光春 Date: Sat May 14 21:38:18 2022 +0800 init diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..511f0fc --- /dev/null +++ b/.drone.yml @@ -0,0 +1,11 @@ +kind: pipeline +type: docker +name: clone + +steps: + - name: test-golang + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -v ./... \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..114804c --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.env +.git +.svn +.idea +.vscode +*.log +gomod.sh +/vendor/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..d9b4677 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +

+Golang Uuid +

+ +📦 Golang Uuid + +[comment]: <> (go) +[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gouuid?status.svg)](https://pkg.go.dev/go.dtapp.net/gouuid) +[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gouuid/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gouuid) +[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gouuid )](https://goreportcard.com/report/go.dtapp.net/gouuid) +[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net/gouuid) + +#### 安装使用 + +```go +go get -v -u go.dtapp.net/gouuid +``` + +#### 导入 + +```go +import ( + "go.dtapp.net/gouuid" +) +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..3b71f7e --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go.dtapp.net/gouuid + +go 1.18 diff --git a/gouuid.go b/gouuid.go new file mode 100644 index 0000000..754fee6 --- /dev/null +++ b/gouuid.go @@ -0,0 +1,18 @@ +package gouuid + +import ( + "crypto/rand" + "fmt" + "time" +) + +// GetUuId 获取唯一ID +func GetUuId() string { + unix32bits := uint32(time.Now().UTC().Unix()) + buff := make([]byte, 12) + numRead, err := rand.Read(buff) + if numRead != len(buff) || err != nil { + return "" + } + return fmt.Sprintf("%x-%x-%x-%x-%x-%x", unix32bits, buff[0:2], buff[2:4], buff[4:6], buff[6:8], buff[8:]) +} diff --git a/gouuid_test.go b/gouuid_test.go new file mode 100644 index 0000000..0104c2a --- /dev/null +++ b/gouuid_test.go @@ -0,0 +1,13 @@ +package gouuid + +import "testing" + +func TestGetUuId(t *testing.T) { + t.Log(GetUuId()) +} + +func BenchmarkGetUuId(b *testing.B) { + for i := 0; i < b.N; i++ { + b.Log(GetUuId()) + } +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..917c41d --- /dev/null +++ b/version.go @@ -0,0 +1,3 @@ +package gouuid + +const Version = "1.0.0"