You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

304 lines
6.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package CommonUtil
import (
"encoding/json"
"fmt"
uuid "github.com/satori/go.uuid"
"net"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"time"
"unsafe"
)
/**
功能:删除多余的空格
作者:黄海
时间2020-04-03
*/
func DeleteExtraSpace(s string) string {
//删除字符串中的多余空格,有多个空格时,仅保留一个空格
s1 := strings.Replace(s, " ", " ", -1) //替换tab为空格
regstr := "\\s{2,}" //两个及两个以上空格的正则表达式
reg, _ := regexp.Compile(regstr) //编译正则表达式
s2 := make([]byte, len(s1)) //定义字符数组切片
copy(s2, s1) //将字符串复制到切片
spc_index := reg.FindStringIndex(string(s2)) //在字符串中搜索
for len(spc_index) > 0 { //找到适配项
s2 = append(s2[:spc_index[0]+1], s2[spc_index[1]:]...) //删除多余空格
spc_index = reg.FindStringIndex(string(s2)) //继续在字符串中搜索
}
return string(s2)
}
/**
功能获取UUID
作者:黄海
时间2020-02-03
*/
func GetUUID() string {
u2 := strings.ToUpper(uuid.NewV4().String())
return u2
}
/**
功能从一个数据库的查询返回结果中获取指定字段的IDS
作者:黄海
时间2020-02-14
*/
func Ids(list []map[string]interface{}, key string) []string {
var ids []string
for i := 0; i < len(list); i++ {
switch list[i][key].(type) {
case string:
ids = append(ids, list[i][key].(string))
break
case int64:
ids = append(ids, strconv.FormatInt(list[i][key].(int64), 10))
}
}
return ids
}
/**
功能:判断是不是合法的日期格式
作者:黄海
时间2020-03-23
*/
func IsDate(date string) bool {
_, err := time.Parse("2006-01-02 15:04:05", date)
if err != nil {
return false
} else {
return true
}
}
/**
功能:获取蛇形命名的字符串
作者:黄海
时间2020-01-22
*/
func GetSnakeCaseStr(str string) string {
array := strings.Split(str, "_")
var result string
for i := 0; i < len(array); i++ {
result = result + strings.ToUpper(array[i][0:1]) + array[i][1:]
}
return result
}
/**
功能将int数据类型转为int64
作者:黄海
时间2020-02-18
*/
func ConvertIntToInt64(i int) int64 {
s := strconv.Itoa(i)
s64, _ := strconv.ParseInt(s, 10, 64)
return s64
}
/**
功能:获取当前的时间戳,精确度纳秒
作者:黄海
时间2020-02-18
*/
func GetCurrentTimestamp() int64 {
return time.Now().UnixNano()
}
/**
功能:获取当前的时间字符串
作者:黄海
时间2020-02-21
*/
func GetCurrentTime() string {
timeStr := time.Now().Format("2006-01-02 15:04:05")
//当前时间的字符串2006-01-02 15:04:05据说是golang的诞生时间固定写法
return timeStr
}
/**
功能:判断所给路径文件/文件夹是否存在
作者: 黄海
时间2020-03-26
*/
func Exists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
/**
功能获取访问的客户机IP
作者:黄海
时间2020-03-26
*/
func RemoteIp(req *http.Request) string {
remoteAddr := req.RemoteAddr
if ip := req.Header.Get("Remote_addr"); ip != "" {
remoteAddr = ip
} else {
remoteAddr, _, _ = net.SplitHostPort(remoteAddr)
}
if remoteAddr == "::1" {
remoteAddr = "127.0.0.1"
}
return remoteAddr
}
/**
功能:判断某一个值是否含在切片之中
作者:黄海
时间2020-03-26
*/
func InArray(need interface{}, haystack interface{}) bool {
switch key := need.(type) {
case int:
for _, item := range haystack.([]int) {
if item == key {
return true
}
}
case string:
for _, item := range haystack.([]string) {
if item == key {
return true
}
}
case int64:
for _, item := range haystack.([]int64) {
if item == key {
return true
}
}
case float64:
for _, item := range haystack.([]float64) {
if item == key {
return true
}
}
default:
return false
}
return false
}
/**
功能转换MapString 到MapInterface
万恶的map[string]string-->map[string]interface{}
作者:黄海
时间2020-03-27
*/
func ConvertMapStringToMapInterface(_bean map[string]string) map[string]interface{} {
n := make(map[string]interface{})
for k, v := range _bean {
n[k] = v
}
return n
}
/**
功能:将整型数组转为字符串型数组
作者:黄海
时间2020-04-12
*/
func ConvertIntegerArrayToStringArray(nums []int) []string {
return strings.Split(strings.Replace(fmt.Sprint(nums)[1:], "]", "", -1), " ")
}
/**
功能将int64转化为string
作者:黄海
时间2020-04-20
*/
func ConvertInt64ToString(int64 int64) string {
return strconv.FormatInt(int64, 10)
}
/**
功能:将一个查询结果转化为字符串,方便返回
作者:黄海
时间:2020-04-22
*/
func SerializeToString(list interface{} )string{
if len(list.([]map[string]interface{}))==0{
return "[]"
}else{
data, _ := json.Marshal(list)
return string(data)
}
}
/**
功能将JSON字符串转化为map数组
作者: 黄海
时间2020-04-17
*/
func ConvertJsonStringToMapArray(data string) []map[string]interface{} {
var m = make([]map[string]interface{}, 0)
str := []byte(data)
json.Unmarshal(str, &m)
return m
}
/**
功能将JSON字符串转化为map数组
作者: 黄海
时间2020-04-17
*/
func ConvertJsonStringToMap(data string) map[string]interface{} {
var m = make(map[string]interface{}, 0)
str := []byte(data)
json.Unmarshal(str, &m)
return m
}
func ConvertJsonStringToBytes(s string) []byte {
x := (*[2]uintptr)(unsafe.Pointer(&s))
h := [3]uintptr{x[0], x[1], x[1]}
return *(*[]byte)(unsafe.Pointer(&h))
}
func ConvertBytesToString(b []byte) string {
return *(*string)(unsafe.Pointer(&b))
}
func StringArrayContain(items []string, item string) bool {
for _, eachItem := range items {
if eachItem == item {
return true
}
}
return false
}
func ConvertInterfaceArrayToStringArray(params []interface{}) []string {
var paramStringArray []string
for _, param := range params {
switch v := param.(type) {
case string:
paramStringArray = append(paramStringArray, v)
case int:
strV := strconv.FormatInt(int64(v), 10)
paramStringArray = append(paramStringArray, strV)
case float64:
strV := strconv.FormatFloat(v, 'f', -1, 64)
paramStringArray = append(paramStringArray, strV)
default:
panic("params type not supported")
}
}
return paramStringArray
}