1 Star 0 Fork 0

Wsage/go-framework

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
other.go 2.53 KB
一键复制 编辑 原始数据 按行查看 历史
sage 提交于 2023-04-10 11:11 . add convert
package utils
import (
"crypto/md5"
"encoding/base64"
"fmt"
"github.com/spf13/cast"
"gopkg.in/yaml.v3"
"io/ioutil"
"strconv"
"strings"
"unsafe"
)
func Base64Encode(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
}
func Base64Decode(s string) string {
decoded, _ := base64.StdEncoding.DecodeString(s)
return string(decoded)
}
func InStringArray(s string, arr []string) bool {
for _, s1 := range arr {
if s == s1 {
return true
}
}
return false
}
func NewMd5Password(password string, secret string) string {
return Md5Sum(secret + Md5Sum(password) + secret)
}
func Md5Sum(s string) string {
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
}
func FmtPageParams(pageNum, pageSize int64) (int64, int64) {
if pageSize <= 0 || pageSize > 1000 {
pageSize = 1000
}
if pageNum <= 0 {
pageNum = 1
}
return pageNum, pageSize
}
//CompareVersion 比较版本,-1:小于,0:相等,1:大于
// v1.1.1 compare v1.2.1
func CompareVersion(ver, cVer string) int {
if strings.HasPrefix(ver, "v") {
ver = ver[1:]
}
if strings.HasPrefix(cVer, "v") {
cVer = cVer[1:]
}
verArr := strings.Split(ver, ".") //当前的版本
cVerArr := strings.Split(cVer, ".") //比较的版本
var max int
if len(verArr) >= len(cVerArr) {
max = len(verArr)
} else {
max = len(cVerArr)
}
for i := 0; i < max; i++ {
cv := 0
v := 0
if i >= len(verArr) {
v = 0
} else {
v, _ = strconv.Atoi(verArr[i])
}
if i >= len(cVerArr) {
cv = 0
} else {
cv, _ = strconv.Atoi(cVerArr[i])
}
if v < cv {
return -1
}
if v > cv {
return 1
}
}
return 0
}
func ReadYaml(data interface{}, yamlContent string) error {
err := yaml.Unmarshal([]byte(yamlContent), data)
if err != nil {
return err
}
return nil
}
func ReadYamlFile(data interface{}, configPath string) error {
yamlFile, err := ioutil.ReadFile(configPath)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, data)
if err != nil {
return err
}
return nil
}
//Interface2Int64 任意值转int64
func Interface2Int64(d interface{}) int64 {
return cast.ToInt64(d)
}
//Interface2Float64 任意值转float64
func Interface2Float64(d interface{}) float64 {
return cast.ToFloat64(d)
}
// StringToBytes converts string to byte slice without a memory allocation.
func StringToBytes(s string) []byte {
return *(*[]byte)(unsafe.Pointer(
&struct {
string
Cap int
}{s, len(s)},
))
}
// BytesToString converts byte slice to string without a memory allocation.
func BytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/scottq/go-framework.git
git@gitee.com:scottq/go-framework.git
scottq
go-framework
go-framework
v1.1.43

搜索帮助