代码拉取完成,页面将自动刷新
package utils
import (
"bufio"
"fmt"
"io"
"log"
"math/rand"
"os"
"time"
)
type Utils struct{}
var DUtils = &Utils{}
// GetRandomString 获取偶数位随机字符串
func (obj *Utils) GetRandomString(n int) (result string) {
rand.Seed(time.Now().UnixNano())
randBytes := make([]byte, n/2)
rand.Read(randBytes)
return fmt.Sprintf("%x", randBytes)
}
// GetRandomNum 获取[N,M]之间的随机整数,N<=M
func (obj *Utils) GetRandomNum(N, M int) (result int) {
if M < N {
log.Fatalln("N必须小于等于M!")
}
time.Sleep(time.Nanosecond)
rand.Seed(time.Now().UnixNano())
return rand.Intn(M-N+1) + N
}
// ReadFileToSlice 将文件中的所有非空行读入切片
func (obj *Utils) ReadFileToSlice(filepath string) (lines []string) {
f, err := os.Open(filepath)
if err != nil {
log.Fatalln(err)
}
defer f.Close()
reader := bufio.NewReader(f)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
if string(line) != "" {
lines = append(lines, string(line))
}
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。