3 Star 1 Fork 0

中光云计算(西安)有限公司 / Doraemon

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Doraemon.go 12.60 KB
一键复制 编辑 原始数据 按行查看 历史
countpoison 提交于 2019-09-30 17:06 . update memcached/main.go
package doraemon
import (
"archive/zip"
"bytes"
"crypto/md5"
"encoding/binary"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"math"
"os/exec"
"runtime"
"strconv"
"time"
)
//转int64,向下取整
func ToInt64Floor(a interface{}) (int64, error) {
switch a.(type) {
case int:
x, _ := a.(int)
return int64(x), nil
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return int64(0), err
}
return int64(math.Floor(f)), err
case float32:
x, _ := a.(float32)
return int64(math.Floor(float64(x))), nil
case float64:
x, _ := a.(float64)
return int64(math.Floor(x)), nil
case []byte:
x, _ := a.([]byte)
bt64 := math.Float64frombits(binary.BigEndian.Uint64(x))
return int64(math.Floor(bt64)), nil
default:
return int64(0), errors.New("must be base type")
}
return int64(0), errors.New("must be base type")
}
//转int64,向上取整
func ToInt64Ceil(a interface{}) (int64, error) {
switch a.(type) {
case int:
x, _ := a.(int)
return int64(x), nil
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return int64(0), err
}
return int64(math.Ceil(f)), err
case float32:
x, _ := a.(float32)
return int64(math.Ceil(float64(x))), nil
case float64:
x, _ := a.(float64)
return int64(math.Ceil(x)), nil
case []byte:
x, _ := a.([]byte)
bt64 := math.Float64frombits(binary.BigEndian.Uint64(x))
return int64(math.Ceil(bt64)), nil
default:
return int64(0), errors.New("must be base type")
}
return int64(0), errors.New("must be base type")
}
//转int64,四舍五入取整
func ToInt64Round(a interface{}) (int64, error) {
switch a.(type) {
case int:
x, _ := a.(int)
return int64(x), nil
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return int64(0), err
}
return int64(math.Round(f)), err
case float32:
x, _ := a.(float32)
return int64(math.Round(float64(x))), nil
case float64:
x, _ := a.(float64)
return int64(math.Round(x)), nil
case []byte:
x, _ := a.([]byte)
var tmp int64
bytesBuffer := bytes.NewBuffer(x)
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int64(tmp), err
default:
return int64(0), errors.New("must be base type")
}
return int64(0), errors.New("must be base type")
}
//转int,向下取整
func ToIntFloor(a interface{}) (int, error) {
switch a.(type) {
case int:
x, _ := a.(int)
return int(x), nil
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return int(0), err
}
return int(math.Floor(f)), err
case float32:
x, _ := a.(float32)
return int(math.Floor(float64(x))), nil
case float64:
x, _ := a.(float64)
return int(math.Floor(x)), nil
case []byte:
x, _ := a.([]byte)
bt64 := math.Float64frombits(binary.BigEndian.Uint64(x))
return int(math.Floor(bt64)), nil
default:
return int(0), errors.New("must be base type")
}
return int(0), errors.New("must be base type")
}
//转int,向上取整
func ToIntCeil(a interface{}) (int, error) {
switch a.(type) {
case int:
x, _ := a.(int)
return int(x), nil
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return int(0), err
}
return int(math.Ceil(f)), err
case float32:
x, _ := a.(float32)
return int(math.Ceil(float64(x))), nil
case float64:
x, _ := a.(float64)
return int(math.Ceil(x)), nil
case []byte:
x, _ := a.([]byte)
bt64 := math.Float64frombits(binary.BigEndian.Uint64(x))
return int(math.Ceil(bt64)), nil
default:
return int(0), errors.New("must be base type")
}
return int(0), errors.New("must be base type")
}
//转int,四舍五入取整
func ToIntRound(a interface{}) (int, error) {
switch a.(type) {
case int64:
x, _ := a.(int)
return int(x), nil
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return int(0), err
}
return int(math.Round(f)), err
case float32:
x, _ := a.(float32)
return int(math.Round(float64(x))), nil
case float64:
x, _ := a.(float64)
return int(math.Round(x)), nil
case []byte:
x, _ := a.([]byte)
bytesBuffer := bytes.NewBuffer(x)
var tmp int32
err := binary.Read(bytesBuffer, binary.BigEndian, &tmp)
return int(tmp), err
default:
return int(0), errors.New("must be base type")
}
return int(0), errors.New("must be base type")
}
//转floate64,rd保留小数点后几位
func ToFloat64Round(a interface{}, rd int) (float64, error) {
switch a.(type) {
case int:
x, _ := a.(int)
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", float64(x))
inst, err := strconv.ParseFloat(floatStr, 64)
return inst, err
case int64:
x, _ := a.(int64)
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", float64(x))
inst, err := strconv.ParseFloat(floatStr, 64)
return inst, err
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return float64(0), err
}
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", f)
inst, err := strconv.ParseFloat(floatStr, 64)
return inst, err
case float32:
x, _ := a.(float32)
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", float64(x))
inst, err := strconv.ParseFloat(floatStr, 64)
return inst, err
case float64:
x, _ := a.(float64)
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", x)
inst, err := strconv.ParseFloat(floatStr, 64)
return inst, err
case []byte:
x, _ := a.([]byte)
bt64 := math.Float64frombits(binary.LittleEndian.Uint64(x))
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", bt64)
inst, err := strconv.ParseFloat(floatStr, 64)
return inst, err
default:
return float64(0), errors.New("must be base type")
}
return float64(0), errors.New("must be base type")
}
//转floate32,rd保留小数点后几位
func ToFloat32Round(a interface{}, rd int) (float32, error) {
switch a.(type) {
case int:
x, _ := a.(int)
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", float64(x))
inst, err := strconv.ParseFloat(floatStr, 64)
return float32(inst), err
case int64:
x, _ := a.(int64)
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", float64(x))
inst, err := strconv.ParseFloat(floatStr, 64)
return float32(inst), err
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return float32(0), err
}
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", f)
inst, err := strconv.ParseFloat(floatStr, 64)
return float32(inst), err
case float32:
x, _ := a.(float32)
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", float64(x))
inst, err := strconv.ParseFloat(floatStr, 64)
return float32(inst), err
case float64:
x, _ := a.(float64)
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", x)
inst, err := strconv.ParseFloat(floatStr, 64)
return float32(inst), err
case []byte:
x, _ := a.([]byte)
bt64 := math.Float64frombits(binary.LittleEndian.Uint64(x))
floatStr := fmt.Sprintf("%."+strconv.Itoa(rd)+"f", bt64)
inst, err := strconv.ParseFloat(floatStr, 64)
return float32(inst), err
default:
return float32(0), errors.New("must be base type")
}
return float32(0), errors.New("must be base type")
}
//转floate64
func ToFloat64(a interface{}) (float64, error) {
switch a.(type) {
case int:
x, _ := a.(int)
return float64(x), nil
case int64:
x, _ := a.(int64)
return float64(x), nil
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return float64(0), err
}
return float64(f), nil
case float32:
x, _ := a.(float32)
return float64(x), nil
case float64:
x, _ := a.(float64)
return x, nil
case []byte:
x, _ := a.([]byte)
bt64 := math.Float64frombits(binary.LittleEndian.Uint64(x))
return bt64, nil
default:
return float64(0), errors.New("must be base type")
}
return float64(0), errors.New("must be base type")
}
//转floate32
func ToFloat32(a interface{}) (float32, error) {
switch a.(type) {
case int:
x, _ := a.(int)
return float32(x), nil
case int64:
x, _ := a.(int64)
return float32(x), nil
case string:
x, _ := a.(string)
f, err := strconv.ParseFloat(x, 64)
if err != nil {
return float32(0), err
}
return float32(f), nil
case float32:
x, _ := a.(float32)
return x, nil
case float64:
x, _ := a.(float64)
return float32(x), nil
case []byte:
x, _ := a.([]byte)
bt64 := math.Float64frombits(binary.LittleEndian.Uint64(x))
return float32(bt64), nil
default:
return float32(0), errors.New("must be base type")
}
return float32(0), errors.New("must be base type")
}
//转string
func ToString(a interface{}) (string, error) {
switch a.(type) {
case int:
x, _ := a.(int)
return strconv.Itoa(x), nil
case int64:
x, _ := a.(int64)
return strconv.FormatInt(x, 10), nil
case string:
x, _ := a.(string)
return x, nil
case float32:
x, _ := a.(float32)
return strconv.FormatFloat(float64(x), 'f', -1, 32), nil
case float64:
x, _ := a.(float64)
return strconv.FormatFloat(x, 'f', -1, 64), nil
case []byte:
x, _ := a.([]byte)
return string(x), nil
default:
return string(""), errors.New("must be base type")
}
return string(""), errors.New("must be base type")
}
//转[]byte
func ToByte(a interface{}) ([]byte, error) {
switch a.(type) {
case int:
x, _ := a.(int)
tmp := int32(x)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, &tmp)
return bytesBuffer.Bytes(), nil
case int64:
x, _ := a.(int64)
tmp := int64(x)
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, &tmp)
return bytesBuffer.Bytes(), nil
case string:
x, _ := a.(string)
return []byte(x), nil
case float32:
x, _ := a.(float32)
bits := math.Float32bits(x)
bytes := make([]byte, 4)
binary.LittleEndian.PutUint32(bytes, bits)
return bytes, nil
case float64:
x, _ := a.(float64)
bits := math.Float64bits(x)
bytes := make([]byte, 4)
binary.LittleEndian.PutUint64(bytes, bits)
return bytes, nil
case []byte:
x, _ := a.([]byte)
return x, nil
default:
return []byte(""), errors.New("must be base type")
}
return []byte(""), errors.New("must be base type")
}
//执行命令,并等待结果,w参数控制是否返回结果
func CmdRun(cmdline string, w bool) (string, error) {
sys := runtime.GOOS
var c *exec.Cmd
if sys == "windows" {
c = exec.Command("cmd", "/C", cmdline)
} else if sys == "linux" {
c = exec.Command("/usr/bin/sh", "-c", cmdline)
} else {
return "", errors.New("system type not found")
}
var cmdreq string
var err error
var cmdbyte []byte
if w {
cmdbyte, err = c.Output()
cmdreq = string(cmdbyte)
} else {
err = c.Run()
}
return cmdreq, err
}
//错误检查,直接panic
func CheckError(err error) {
if err != nil {
panic(err)
}
}
//time.sleep的简化写法,毫秒为单位
func Sleep(val int) {
time.Sleep(time.Duration(val) * time.Millisecond)
}
//将表现为11或13位数字的字符串的任意类型数据,转化为一个时间格式化的字符串
func TimeFormatTo(val interface{}, timestr string) (string, error) {
var valstr string
switch val.(type) {
case string:
valstr, _ = val.(string)
case int64:
x, _ := val.(int64)
valstr, _ = ToString(x)
case int:
x, _ := val.(int)
valstr, _ = ToString(x)
case float64:
x, _ := val.(float64)
valstr, _ = ToString(x)
case float32:
x, _ := val.(float32)
valstr, _ = ToString(x)
case []byte:
x, _ := val.([]byte)
valstr, _ = ToString(x)
default:
return "", errors.New("val type test failed")
}
if len(valstr) == 11 {
int64s, err := ToInt64Floor(valstr)
float64s, err := ToFloat64(int64s)
int64s, err = ToInt64Round(float64s)
return time.Unix(int64s, 0).Format(timestr), err
} else if len(valstr) == 13 {
int64s, err := ToInt64Floor(valstr)
float64s, err := ToFloat64(int64s / 1000)
int64s, err = ToInt64Round(float64s)
return time.Unix(int64s, 0).Format(timestr), err
} else {
return "", errors.New("val len() not 11 or 13 failed")
}
return "", errors.New("format failed")
}
//将表现为11或13位数字的字符串的任意类型数据,转化为一个"2006-01-02 15:04:05"时间格式化的字符串
func TimeFormat(val interface{}) (string, error) {
return TimeFormatTo(val, "2006-01-02 15:04:05")
}
//文件md5检查
func Md5sum(file string) string {
f, err := os.Open(file)
if err != nil {
return ""
}
defer f.Close()
h := md5.New()
_, err = io.Copy(h, f)
if err != nil {
return ""
}
return fmt.Sprintf("%x", h.Sum(nil))
}
func CompressZip(list []string, zipname string) error {
fzip, _ := os.Create(zipname)
w := zip.NewWriter(fzip)
defer w.Close()
for _, file := range list {
fw, _ := w.Create(file)
filecontent, err := ioutil.ReadFile(file)
if err != nil {
return err
}
_, err = fw.Write(filecontent)
if err != nil {
return err
}
}
return nil
}
Go
1
https://gitee.com/countpoison/Doraemon.git
git@gitee.com:countpoison/Doraemon.git
countpoison
Doraemon
Doraemon
f6c0e8d84ec1

搜索帮助