Compare commits

...

6 Commits

2
.gitignore vendored

@ -4,6 +4,6 @@
.idea
.vscode
*.log
gomod.sh
go_mod.sh
*_test.go
/vendor/

@ -1,8 +1,8 @@
<h1>
<a href="https://www.dtapp.net/">Golang String</a>
<a href="https://www.dtapp.net/">Golang</a>
</h1>
📦 Golang String
📦 Golang 字符串
[comment]: <> (go)
[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gostring?status.svg)](https://pkg.go.dev/go.dtapp.net/gostring)
@ -12,6 +12,6 @@
#### 安装
```go
go get -v -u go.dtapp.net/gostring
```shell
go get -v -u go.dtapp.net/gostring@v1.0.14
```

@ -1,3 +0,0 @@
package gostring
const Version = "1.0.9"

@ -1,203 +1,58 @@
package gostring
import (
"errors"
"fmt"
"go.dtapp.net/gorandom"
"go.dtapp.net/gotime"
)
// GenerateId 生成18编号
// GenerateId 生成18编号
func GenerateId(customId string) string {
currentTime := gotime.Current().Format()
one, err := generateIdOne(customId, currentTime)
one, err := generateIdOne(customId, currentTime, 18)
if err == nil {
return one
}
two, err := generateIdTwo(customId, currentTime)
two, err := generateIdTwo(customId, currentTime, 18)
if err == nil {
return two
}
three, err := generateIdThree(customId, currentTime)
three, err := generateIdThree(customId, currentTime, 18)
if err == nil {
return three
}
four, err := generateIdFour(customId, currentTime)
four, err := generateIdFour(customId, currentTime, 18)
if err == nil {
return four
}
five, err := generateIdFive(customId, currentTime)
five, err := generateIdFive(customId, currentTime, 18)
if err == nil {
return five
}
six, err := generateIdSix(customId)
six, err := generateIdSix(customId, 18)
return six
}
// GenerateIdAndTime 生成18编号
// GenerateIdAndTime 生成18编号
func GenerateIdAndTime(customId, customTime string) string {
one, err := generateIdOne(customId, customTime)
one, err := generateIdOne(customId, customTime, 18)
if err == nil {
return one
}
two, err := generateIdTwo(customId, customTime)
two, err := generateIdTwo(customId, customTime, 18)
if err == nil {
return two
}
three, err := generateIdThree(customId, customTime)
three, err := generateIdThree(customId, customTime, 18)
if err == nil {
return three
}
four, err := generateIdFour(customId, customTime)
four, err := generateIdFour(customId, customTime, 18)
if err == nil {
return four
}
five, err := generateIdFive(customId, customTime)
five, err := generateIdFive(customId, customTime, 18)
if err == nil {
return five
}
six, err := generateIdSix(customId)
six, err := generateIdSix(customId, 18)
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
}

@ -0,0 +1,144 @@
package gostring
import (
"errors"
"fmt"
"go.dtapp.net/gorandom"
"go.dtapp.net/gotime"
)
// 生成18位时间[年月日时分]唯一编号
func generateIdOne(customId, setTime string, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度`
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, dataLength int) (string, error) {
var (
newRandomLength = 0 // 随机数据长度
customIdLength = len(customId) // 自定义长度
)
const (
randomLength = 5 // 随机数据长度
)
// 重新计算随机数据长度
newRandomLength = dataLength - customIdLength
if (customIdLength > dataLength) || (customIdLength == dataLength) || (newRandomLength < randomLength) {
return "", errors.New("没有满足条件")
}
return fmt.Sprintf("%v%s", customId, gorandom.Numeric(newRandomLength)), nil
}

@ -0,0 +1,56 @@
package gostring
import "go.dtapp.net/gotime"
// GenerateIdLength 生成自定义长度编号
func GenerateIdLength(customId string, dataLength int) string {
currentTime := gotime.Current().Format()
one, err := generateIdOne(customId, currentTime, dataLength)
if err == nil {
return one
}
two, err := generateIdTwo(customId, currentTime, dataLength)
if err == nil {
return two
}
three, err := generateIdThree(customId, currentTime, dataLength)
if err == nil {
return three
}
four, err := generateIdFour(customId, currentTime, dataLength)
if err == nil {
return four
}
five, err := generateIdFive(customId, currentTime, dataLength)
if err == nil {
return five
}
six, err := generateIdSix(customId, dataLength)
return six
}
// GenerateIdAndTimeLength 生成自定义长度编号
func GenerateIdAndTimeLength(customId, customTime string, dataLength int) string {
one, err := generateIdOne(customId, customTime, dataLength)
if err == nil {
return one
}
two, err := generateIdTwo(customId, customTime, dataLength)
if err == nil {
return two
}
three, err := generateIdThree(customId, customTime, dataLength)
if err == nil {
return three
}
four, err := generateIdFour(customId, customTime, dataLength)
if err == nil {
return four
}
five, err := generateIdFive(customId, customTime, dataLength)
if err == nil {
return five
}
six, err := generateIdSix(customId, dataLength)
return six
}

@ -0,0 +1,15 @@
package gostring
func GetDefault(key, defVal any) any {
if key != nil {
return key
}
return defVal
}
func GetStringDefault(key, defVal string) string {
if key != "" {
return key
}
return defVal
}

@ -1,8 +1,26 @@
module go.dtapp.net/gostring
go 1.18
go 1.22
require (
go.dtapp.net/gorandom v1.0.1
go.dtapp.net/gotime v1.0.5
go.dtapp.net/gojson v1.0.4
go.dtapp.net/gorandom v1.0.3
go.dtapp.net/gotime v1.0.8
)
require (
github.com/basgys/goxml2json v1.1.0 // indirect
github.com/bytedance/sonic v1.11.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

@ -1,4 +1,67 @@
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.5 h1:12aNgB2ULpP6QgQHEUkLilZ4ASvhpFxMFQkBwn0par8=
go.dtapp.net/gotime v1.0.5/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY=
github.com/basgys/goxml2json v1.1.0 h1:4ln5i4rseYfXNd86lGEB+Vi652IsIXIvggKM/BhUKVw=
github.com/basgys/goxml2json v1.1.0/go.mod h1:wH7a5Np/Q4QoECFIU8zTQlZwZkrilY0itPfecMw41Dw=
github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow=
github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.11.3 h1:jRN+yEjakWh8aK5FzrciUHG8OFXK+4/KrAX/ysEtHAA=
github.com/bytedance/sonic v1.11.3/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0=
github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
go.dtapp.net/gojson v1.0.4 h1:9en9iyOOLWoEIo2eKhqt3/Djh/3HhwsTpXxgI9efPRo=
go.dtapp.net/gojson v1.0.4/go.mod h1:G9CMVzNSRkbNzIic/vJqHCOyKtw6BW2YM8Vyn64zfM0=
go.dtapp.net/gorandom v1.0.3 h1:6RNDFMJfLlHFR98c2tbecaMzg4vEIHyEpduMBUdd0jc=
go.dtapp.net/gorandom v1.0.3/go.mod h1:Qd6ywCSrk7sCkh9OdR0wygcbQBFt53WDIrx8rqGBvis=
go.dtapp.net/gotime v1.0.8 h1:ngKBp7j3Ime1t2jU94gUeshPX1StV5E2x4STlzDMmLU=
go.dtapp.net/gotime v1.0.8/go.mod h1:BQT911XSfaJDBYPiEKGFwMjvgnZtx1F3nWu4GTg5ISY=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.7.0 h1:pskyeJh/3AmoQ8CPE95vxHLqp1G1GfGNXTmcl9NEKTc=
golang.org/x/arch v0.7.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

@ -1,7 +1,7 @@
package gostring
import (
"math/rand"
"math/rand/v2"
"time"
)
@ -44,11 +44,12 @@ func GenerateRandomNumber(start, end, count int, dFun func(num int) bool) []int
nums := make([]int, 0)
// 随机数生成器,加入时间戳保证每次生成的随机数不一样
r := rand.New(rand.NewSource(time.Now().UnixNano()))
seed := time.Now().UnixNano() // rand内部运算的随机数
r := rand.New(rand.NewPCG(uint64(seed), uint64(seed))) // rand计算得到的随机数
for len(nums) < count {
// 生成随机数
num := r.Intn(end-start) + start
num := r.IntN(end-start) + start
// 查重
exist := false

@ -0,0 +1,76 @@
package gostring
import (
"math/rand/v2"
"time"
)
// GenerateRandomString 生成count个长度length不重复的随机数
func GenerateRandomString(length, count int) []string {
return GenerateRandomStringFunc(length, count, func(num string) bool {
return false
})
}
// GenerateRandomStringFunc 生成count个长度length不重复的随机数支持外部查询
func GenerateRandomStringFunc(length, count int, dFun func(num string) bool) []string {
var fI int = 0
startStr := "1"
endStr := "9"
alphabet := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
seed := time.Now().UnixNano() // rand内部运算的随机数
rand.New(rand.NewPCG(uint64(seed), uint64(seed))) // rand计算得到的随机数
for {
if fI+2 > length {
break
}
startStr += "0"
endStr += "9"
fI = fI + 1
}
return GenerateRandomStringNumber(ToInt(startStr), ToInt(endStr), count, alphabet, length, dFun)
}
// GenerateRandomStringNumber 生成count个[start,end)结束的不重复的随机数
func GenerateRandomStringNumber(start, end, count int, alphabet string, length int, dFun func(num string) bool) []string {
// 范围检查
if end < start || len(alphabet) == 0 || (end-start) < count {
return nil
}
// 存放结果的slice
results := make([]string, 0)
for len(results) < count {
// 生成随机字符串
str := make([]byte, length)
for i := range str {
str[i] = alphabet[rand.IntN(len(alphabet))]
}
// 查重
exist := false
for _, v := range results {
if v == string(str) {
exist = true
break
}
}
if !exist {
isExist := dFun(string(str))
if !isExist {
results = append(results, string(str))
}
}
}
return results
}

@ -5,9 +5,11 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"go.dtapp.net/gojson"
"strconv"
"strings"
"unicode/utf8"
"unsafe"
)
// ToString 转换成string
@ -150,3 +152,38 @@ func NumericalToString(value interface{}) (string, bool) {
}
return val, true
}
func StringToBytes(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
func BytesToString(b []byte) string {
return unsafe.String(&b[0], len(b))
}
// ToUpper 转大写
func ToUpper(s string) string {
return strings.ToUpper(s)
}
// ToLower 转小写
func ToLower(s string) string {
return strings.ToLower(s)
}
func GetString(i interface{}) string {
switch v := i.(type) {
case string:
return v
case []byte:
return string(v)
case int:
return strconv.Itoa(v)
case bool:
return strconv.FormatBool(v)
default:
bytes, _ := gojson.Marshal(v)
return string(bytes)
}
}

@ -0,0 +1,45 @@
package gostring
import (
"strings"
)
// CompleteUrlHttp 补全 URL
func CompleteUrlHttp(url string) string {
if url == "" {
return url
}
if strings.HasPrefix(url, "http://") {
return url
}
if strings.HasPrefix(url, "//") {
url = "http:" + url
} else if strings.HasPrefix(url, "://") {
url = "http" + url
} else if strings.HasPrefix(url, "https://") {
url = Replace(url, "https://", "http://")
} else {
url = "http://" + url
}
return url
}
// CompleteUrlHttps 补全 URL
func CompleteUrlHttps(url string) string {
if url == "" {
return url
}
if strings.HasPrefix(url, "https://") {
return url
}
if strings.HasPrefix(url, "//") {
url = "https:" + url
} else if strings.HasPrefix(url, "://") {
url = "https" + url
} else if strings.HasPrefix(url, "http://") {
url = Replace(url, "http://", "https://")
} else {
url = "https://" + url
}
return url
}

@ -0,0 +1,3 @@
package gostring
const Version = "1.0.14"
Loading…
Cancel
Save