更新文档,代码;

非 *nix 平台不再使用 bubble包, 减小大小
This commit is contained in:
hahafool
2022-04-22 20:11:02 +08:00
parent f5803d1ba0
commit e44e885282
5 changed files with 45 additions and 25 deletions

View File

@@ -3,31 +3,9 @@ package utils
import (
"math/rand"
"strings"
"github.com/tjarratt/babble"
"go.uber.org/zap"
)
func GetRandomWord() (result string) {
//babbler包 在 系统中 没有 /usr/share/dict/words 且不是windows 时会panic
defer func() {
if r := recover(); r != nil {
if ce := CanLogErr("getRandomWord babble panic"); ce != nil {
ce.Write(zap.Any("err:", r))
}
result = GenerateRandomString()
}
}()
babbler := babble.NewBabbler()
babbler.Count = 1
result = babbler.Babble()
return
}
//6-12 字节的字符串
//6-11 字节的字符串
func GenerateRandomString() string {
lenth := rand.Intn(6) + 6

9
utils/strings_others.go Normal file
View File

@@ -0,0 +1,9 @@
//go:build !(darwin || dragonfly || freebsd || linux || netbsd || openbsd)
package utils
func GetRandomWord() string {
// 在非 unix/linux 系统下, 不必使用 babble包因为该包太大了占用空间.
return GenerateRandomString()
}

28
utils/strings_unix.go Normal file
View File

@@ -0,0 +1,28 @@
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
// +build darwin dragonfly freebsd linux netbsd openbsd
package utils
import (
"github.com/tjarratt/babble"
"go.uber.org/zap"
)
func GetRandomWord() (result string) {
//babbler包 在 系统中 没有 /usr/share/dict/words 且不是windows 时会panic
defer func() {
if r := recover(); r != nil {
if ce := CanLogErr("getRandomWord babble panic"); ce != nil {
ce.Write(zap.Any("err:", r))
}
result = GenerateRandomString()
}
}()
babbler := babble.NewBabbler()
babbler.Count = 1
result = babbler.Babble()
return
}