mirror of
https://github.com/eolinker/apinto
synced 2025-11-02 12:54:19 +08:00
优化随机字符串方法
This commit is contained in:
@@ -86,7 +86,7 @@ func Md5(encodeString string) string {
|
||||
}
|
||||
|
||||
//GetRandomString 生成随机字符串
|
||||
func GetRandomString(num int) string {
|
||||
func GetRandomStringBack(num int) string {
|
||||
str := "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"
|
||||
bytes := []byte(str)
|
||||
result := []byte{}
|
||||
@@ -97,6 +97,22 @@ func GetRandomString(num int) string {
|
||||
return string(result)
|
||||
}
|
||||
|
||||
const str = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"
|
||||
var (
|
||||
randomBytes = []byte(str)
|
||||
randT = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
)
|
||||
|
||||
//GetRandomString 生成随机字符串
|
||||
func GetRandomString(num int) string {
|
||||
|
||||
result := make([]byte,num)
|
||||
|
||||
for i := 0; i < num; i++ {
|
||||
result[i] = randomBytes[int(randT.Int31())%num]
|
||||
}
|
||||
return string(result)
|
||||
}
|
||||
//CheckFileIsExist 判断文件是否存在 存在返回 true 不存在返回false
|
||||
func CheckFileIsExist(filename string) bool {
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
|
||||
23
utils/function_test.go
Normal file
23
utils/function_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
|
||||
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
|
||||
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
|
||||
* Vestibulum commodo. Ut rhoncus gravida arcu.
|
||||
*/
|
||||
|
||||
package utils
|
||||
|
||||
import "testing"
|
||||
|
||||
|
||||
func BenchmarkV1(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
GetRandomString(16) // run fib(30) b.N times
|
||||
}
|
||||
}
|
||||
func BenchmarkV2(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
GetRandomStringV2(16) // run fib(30) b.N times
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user