1 Star 0 Fork 0

李小程/blog-lib

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
functions.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
李小程 提交于 2025-07-20 02:16 +08:00 . fix
package components
import (
"crypto/md5"
"encoding/hex"
"math/rand"
"net/http"
"time"
)
// Sequence 生成序列
// n: 序列长度
func Sequence(n int) []int {
result := make([]int, n)
for i := 0; i < n; i++ {
result[i] = i + 1
}
return result
}
// Md5 生成32位MD5哈希值
// text: 要加密的文本
func Md5(text string) string {
ctx := md5.New()
ctx.Write([]byte(text))
return hex.EncodeToString(ctx.Sum(nil))
}
// GetRandomString 生成随机字符串
// length: 字符串长度
func GetRandomString(length uint) string {
str := "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var i uint
for i = 0; i < length; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
// DatetimeToTimeZone datetime格式时间转时间戳
// timeString: 时间字符串,格式为"2006-01-02 15:04:05"
func DatetimeToTimeZone(timeString string) (timeZone int64) {
timeLayout := "2006-01-02 15:04:05"
tm2, err := time.Parse(timeLayout, timeString)
if err != nil {
return 0
}
timeZone = tm2.Unix()
return
}
// GetCurrentUrl 获取当前请求的完整URL
func GetCurrentUrl(r *http.Request) string {
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
return scheme + "://" + r.Host + r.RequestURI
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/xchengli/blog-lib.git
git@gitee.com:xchengli/blog-lib.git
xchengli
blog-lib
blog-lib
7bad096cf672

搜索帮助