/*
* @,@Author: ,: lxcos
* @,@Date: ,: 2020-10-31 17:36:39
*/
package main
import (
"github.com/sirupsen/logrus"
)
func main() {
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
customFormatter.FullTimestamp = true
logrus.SetFormatter(customFormatter)
logrus.Info("Hello Walrus after FullTimestamp=true")
}
关于字符串拼接效率:
单词调用字符串拼接性能 + > strings.Join > bytes.Buffer > fmt.Sprintf
大量数据拼接推荐bytes.buffer{},bytes.buffer的性能是非常高的。
随机字符串生成器:
func RandomString(n int, allowedChars ...[]rune) string {
var letters []rune
if len(allowedChars) == 0 {
letters = defaultLetters
} else {
letters = allowedChars[0]
}
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
-------------------------------------------------------------------------------------
func GetNoceStr() string {
char := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=_"
rand.NewSource(time.Now().UnixNano()) // 产生随机种子
var s bytes.Buffer
for i := 0; i < 32; i ++ {
s.WriteByte(char[rand.Int63() % int64(len(char))])
}
return s.String()
}
网友评论