Compare commits
10 Commits
Author | SHA1 | Date |
---|---|---|
dtapps | 93d82d75d0 | 1 month ago |
dtapps | ae97233c8c | 4 months ago |
dtapps | a5dd99a467 | 4 months ago |
dtapps | 351c0d037a | 4 months ago |
dtapps | 05be487085 | 4 months ago |
李光春 | 29ce595d20 | 2 years ago |
李光春 | 266edec86a | 2 years ago |
李光春 | 0626f9f0f3 | 2 years ago |
李光春 | eab2cb4793 | 2 years ago |
李光春 | dac1c863d7 | 2 years ago |
@ -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
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
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)))
|
||||
}
|
||||
}
|
@ -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.4
|
||||
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.4 h1:gOOkGKfhJwX+dsGHvnF2est09Oqvk7cihJkePddJ5a8=
|
||||
go.dtapp.net/gotime v1.0.4/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,66 +0,0 @@
|
||||
package gostring
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
str := "iPhone 11 Pro Max<iPhone12,5>"
|
||||
fmt.Printf("%d\n", strings.LastIndex(str, "<"))
|
||||
fmt.Printf("%d\n", strings.LastIndex(str, "("))
|
||||
fmt.Printf("%d\n", strings.LastIndex("iPad (6th generation, WiFi)<iPad7,5>", "<"))
|
||||
fmt.Printf("%d\n", strings.LastIndex("iPad (6th generation, WiFi)<iPad7,5>", "("))
|
||||
}
|
||||
|
||||
func TestToInt64(t *testing.T) {
|
||||
t.Log(ToInt64("120"))
|
||||
t.Log(ToInt64("120.9"))
|
||||
t.Log(strings.Contains("120", ","))
|
||||
t.Log(strings.Contains("120,1", ","))
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
str := "wx6566ef69e8738ad9"
|
||||
fmt.Println(strings.Contains(str, "wx"))
|
||||
myString := "www.5lmh.com"
|
||||
if strings.HasPrefix(myString, "www") {
|
||||
fmt.Println("Hello to you too")
|
||||
} else {
|
||||
fmt.Println("Goodbye")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSplit(t *testing.T) {
|
||||
t.Log(Split("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20", ","))
|
||||
t.Log(Split(",10,", ","))
|
||||
t.Log(len(Split(",10,", ",")))
|
||||
t.Log(Split(",10,", ",")[1 : len(Split(",1,", ","))-1])
|
||||
t.Log(len(Split(",10,", ",")[1 : len(Split(",1,", ","))-1]))
|
||||
t.Log(Contains("", ","))
|
||||
t.Log(len([]string{}))
|
||||
t.Log(Split("/pages/preferential_recharge/goods_details?goods_id=G02022062517457120", "="))
|
||||
}
|
||||
|
||||
func TestContains(t *testing.T) {
|
||||
t.Log(Contains("1", ","))
|
||||
t.Log(Contains("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20", ","))
|
||||
}
|
||||
|
||||
func TestToFloat64(t *testing.T) {
|
||||
t.Log(ToFloat64("120"))
|
||||
t.Log(ToFloat64("120.9"))
|
||||
t.Log(ToFloat64("100.100.100"))
|
||||
if "100.100.100" > "111.111.111" {
|
||||
log.Println("1")
|
||||
} else {
|
||||
log.Println("2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReplace(t *testing.T) {
|
||||
t.Log(Replace("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20", ",", "|"))
|
||||
t.Log(Replace("/v1/a/{agent_user_id}/d/g", "{agent_user_id}", "A102DFB78FADE96F1E"))
|
||||
}
|
@ -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
|
||||
}
|
@ -1,15 +0,0 @@
|
||||
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())
|
||||
}
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
package gostring
|
||||
|
||||
const Version = "1.0.6"
|
||||
const Version = "1.0.14"
|
||||
|
@ -1,13 +0,0 @@
|
||||
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)
|
||||
}
|
||||
}
|
Loading…
Reference in new issue