diff --git a/.gitignore b/.gitignore index 8f33b64..114804c 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ .idea .vscode *.log -gomod.sh \ No newline at end of file +gomod.sh +/vendor/ diff --git a/README.md b/README.md index fc529be..4a11776 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Golang String -📦 Golang 字符串处理 +📦 Golang String [comment]: <> (go) [![godoc](https://pkg.go.dev/badge/go.dtapp.net/gostring?status.svg)](https://pkg.go.dev/go.dtapp.net/gostring) diff --git a/custom.go b/custom.go new file mode 100644 index 0000000..103ada9 --- /dev/null +++ b/custom.go @@ -0,0 +1,203 @@ +package gostring + +import ( + "errors" + "fmt" + "go.dtapp.net/gorandom" + "go.dtapp.net/gotime" +) + +// GenerateId 生成18一编号 +func GenerateId(customId string) string { + currentTime := gotime.Current().Format() + one, err := generateIdOne(customId, currentTime) + if err == nil { + return one + } + two, err := generateIdTwo(customId, currentTime) + if err == nil { + return two + } + three, err := generateIdThree(customId, currentTime) + if err == nil { + return three + } + four, err := generateIdFour(customId, currentTime) + if err == nil { + return four + } + five, err := generateIdFive(customId, currentTime) + if err == nil { + return five + } + six, err := generateIdSix(customId) + return six +} + +// GenerateIdAndTime 生成18一编号 +func GenerateIdAndTime(customId, customTime string) string { + one, err := generateIdOne(customId, customTime) + if err == nil { + return one + } + two, err := generateIdTwo(customId, customTime) + if err == nil { + return two + } + three, err := generateIdThree(customId, customTime) + if err == nil { + return three + } + four, err := generateIdFour(customId, customTime) + if err == nil { + return four + } + five, err := generateIdFive(customId, customTime) + if err == nil { + return five + } + six, err := generateIdSix(customId) + return six +} + +// 生成18位时间[年月日时分]唯一编号 +func generateIdOne(customId, setTime string) (string, error) { + + var ( + newRandomLength = 0 // 随机数据长度 + customIdLength = len(customId) // 自定义长度 + ) + + const ( + randomLength = 5 // 随机数据长度 + dataLength = 18 // 默认数据长度 + dateFormat = "200601021504" // 时间格式 + dateFormatLength = len(dateFormat) // 时间格式长度 + ) + + // 重新计算随机数据长度 + newRandomLength = dataLength - (dateFormatLength + customIdLength) + + if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { + return "", errors.New("没有满足条件") + } + return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil +} + +// 生成18位时间[年月日时]唯一编号 +func generateIdTwo(customId, setTime string) (string, error) { + + var ( + newRandomLength = 0 // 随机数据长度 + customIdLength = len(customId) // 自定义长度 + ) + + const ( + randomLength = 5 // 随机数据长度 + dataLength = 18 // 默认数据长度 + dateFormat = "2006010215" // 时间格式 + dateFormatLength = len(dateFormat) // 时间格式长度 + ) + + // 重新计算随机数据长度 + newRandomLength = dataLength - (dateFormatLength + customIdLength) + + if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { + return "", errors.New("没有满足条件") + } + return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil +} + +// 生成18位时间[年月日]唯一编号 +func generateIdThree(customId, setTime string) (string, error) { + + var ( + newRandomLength = 0 // 随机数据长度 + customIdLength = len(customId) // 自定义长度 + ) + + const ( + randomLength = 5 // 随机数据长度 + dataLength = 18 // 默认数据长度 + dateFormat = "20060102" // 时间格式 + dateFormatLength = len(dateFormat) // 时间格式长度 + ) + + // 重新计算随机数据长度 + newRandomLength = dataLength - (dateFormatLength + customIdLength) + + if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { + return "", errors.New("没有满足条件") + } + return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil +} + +// 生成18位时间[年月]唯一编号 +func generateIdFour(customId, setTime string) (string, error) { + + var ( + newRandomLength = 0 // 随机数据长度 + customIdLength = len(customId) // 自定义长度 + ) + + const ( + randomLength = 5 // 随机数据长度 + dataLength = 18 // 默认数据长度 + dateFormat = "200601" // 时间格式 + dateFormatLength = len(dateFormat) // 时间格式长度 + ) + + // 重新计算随机数据长度 + newRandomLength = dataLength - (dateFormatLength + customIdLength) + + if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { + return "", errors.New("没有满足条件") + } + return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil +} + +// 生成18位时间[年]唯一编号 +func generateIdFive(customId, setTime string) (string, error) { + + var ( + newRandomLength = 0 // 随机数据长度 + customIdLength = len(customId) // 自定义长度 + ) + + const ( + randomLength = 5 // 随机数据长度` + dataLength = 18 // 默认数据长度 + dateFormat = "2006" // 时间格式 + dateFormatLength = len(dateFormat) // 时间格式长度 + ) + + // 重新计算随机数据长度 + newRandomLength = dataLength - (dateFormatLength + customIdLength) + + if (dateFormatLength+customIdLength > dataLength) || (dateFormatLength+customIdLength == dataLength) || (newRandomLength < randomLength) { + return "", errors.New("没有满足条件") + } + return fmt.Sprintf("%v%s%s", customId, gotime.SetCurrentParse(setTime).SetFormat(dateFormat), gorandom.Numeric(newRandomLength)), nil +} + +// 生成18位随机唯一编号 +func generateIdSix(customId string) (string, error) { + + var ( + newRandomLength = 0 // 随机数据长度 + customIdLength = len(customId) // 自定义长度 + ) + + const ( + randomLength = 5 // 随机数据长度 + dataLength = 18 // 默认数据长度 + ) + + // 重新计算随机数据长度 + newRandomLength = dataLength - customIdLength + + if (customIdLength > dataLength) || (customIdLength == dataLength) || (newRandomLength < randomLength) { + return "", errors.New("没有满足条件") + } + return fmt.Sprintf("%v%s", customId, gorandom.Numeric(newRandomLength)), nil +} diff --git a/custom_test.go b/custom_test.go new file mode 100644 index 0000000..808d49e --- /dev/null +++ b/custom_test.go @@ -0,0 +1,57 @@ +package gostring + +import ( + "strconv" + "testing" +) + +func TestGenerateId(t *testing.T) { + t.Log(GenerateId("")) +} + +func TestGenerateIdAndTime(t *testing.T) { + var data = []string{ + "2022-04-26 15:15:15.244", + "2022-04-26 15:21:39.852", + "2022-04-26 16:45:12.915", + "2022-04-26 16:45:51.664", + "2022-04-26 16:46:10.647", + "2022-04-26 16:46:35.952", + "2022-04-26 16:47:02.211", + "2022-04-26 16:48:46.841", + "2022-04-26 16:49:05.583", + "2022-04-26 16:49:38.909", + "2022-04-26 16:50:37.448", + "2022-04-26 16:50:58.763", + "2022-04-26 16:51:14.672", + "2022-04-26 16:52:06.194", + "2022-04-26 16:52:33.746", + "2022-04-26 16:52:56.281", + "2022-04-26 16:53:21.239", + "2022-04-26 16:53:50.016", + "2022-04-26 16:54:17.313", + "2022-04-26 16:54:47.056", + "2022-04-26 16:55:12.822", + "2022-04-26 16:55:45.031", + "2022-04-26 16:56:38.678", + "2022-04-26 16:57:00.522", + "2022-04-26 16:57:33.240", + "2022-04-26 17:28:37.719", + } + for _, v := range data { + t.Log(GenerateIdAndTime("P0", v)) + } +} + +func BenchmarkGenerateId(b *testing.B) { + for i := 0; i < b.N; i++ { + b.Logf("---------%v---------", i) + b.Log(GenerateId("")) + b.Log(GenerateId("Q")) + b.Log(GenerateId("Q1")) + b.Log(GenerateId("Q10000")) + b.Log(GenerateId("Q172676")) + b.Log(GenerateId("Q17267650")) + b.Log(GenerateId(strconv.Itoa(i))) + } +} diff --git a/go.mod b/go.mod index c789877..8936eb0 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module go.dtapp.net/gostring go 1.18 + +require ( + go.dtapp.net/gorandom v1.0.1 + go.dtapp.net/gotime v1.0.4 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..67120ca --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +go.dtapp.net/gorandom v1.0.1 h1:IWfMClh1ECPvyUjlqD7MwLq4mZdUusD1qAwAdsvEJBs= +go.dtapp.net/gorandom v1.0.1/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8= +go.dtapp.net/gotime v1.0.4 h1:gOOkGKfhJwX+dsGHvnF2est09Oqvk7cihJkePddJ5a8= +go.dtapp.net/gotime v1.0.4/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY= diff --git a/only.go b/only.go new file mode 100644 index 0000000..e2dfa57 --- /dev/null +++ b/only.go @@ -0,0 +1,8 @@ +package gostring + +type Only struct { +} + +func NewOnly() *Only { + return &Only{} +} diff --git a/gostring.go b/string.go similarity index 100% rename from gostring.go rename to string.go diff --git a/gostring_test.go b/string_test.go similarity index 100% rename from gostring_test.go rename to string_test.go diff --git a/uuid.go b/uuid.go new file mode 100644 index 0000000..d3c6e8a --- /dev/null +++ b/uuid.go @@ -0,0 +1,18 @@ +package gostring + +import ( + "crypto/rand" + "fmt" + "time" +) + +// GetUuId 由 32 个十六进制数字组成,以 6 个组显示,由连字符 - 分隔 +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/uuid_test.go b/uuid_test.go new file mode 100644 index 0000000..991fcda --- /dev/null +++ b/uuid_test.go @@ -0,0 +1,15 @@ +package gostring + +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 index e38a843..f6076d7 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package gostring -const Version = "1.0.5" +const Version = "1.0.6" diff --git a/version_test.go b/version_test.go new file mode 100644 index 0000000..adbab51 --- /dev/null +++ b/version_test.go @@ -0,0 +1,13 @@ +package gostring + +import "testing" + +func TestVersion(t *testing.T) { + t.Log(Version) +} + +func BenchmarkVersion(b *testing.B) { + for i := 0; i < b.N; i++ { + b.Log(Version) + } +}