myql操作
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.

167 lines
3.1 KiB

package tencentdb
import (
"fmt"
"log"
"strconv"
"strings"
)
/*
* 连接多个字符串
* 2019/05/05
*/
func StringJoin(s ...string) string {
var build strings.Builder
if len(s) > 0 {
for _, v := range s {
build.WriteString(v)
}
}
return build.String()
}
/**
* 字符串转大驼峰 ios_bbbbbbbb -> IosBbbbbbbbb
*/
func StrFirstToUpper(str string) string {
str = strings.ReplaceAll(str, "`", "")
temp := strings.Split(str, "_")
var upperStr string
for y := 0; y < len(temp); y++ {
vv := []rune(temp[y])
for i := 0; i < len(vv); i++ {
if i == 0 {
vv[i] -= 32
upperStr += string(vv[i])
} else {
upperStr += string(vv[i])
}
}
}
return upperStr
}
func ToString(v interface{}) string {
var value string
switch v.(type) {
case string:
value = v.(string)
case int:
value = strconv.Itoa(v.(int))
case float64:
value = strconv.FormatFloat(v.(float64), 'f', 2, 64)
case float32:
value = strconv.FormatFloat(float64(v.(float32)), 'f', 2, 64)
case int64:
value = strconv.FormatInt(v.(int64), 10)
case []uint8:
value = string(v.([]uint8))
// case []byte:
// value = string(v.([]byte))
case interface{}:
value = v.(string)
case nil:
value = ""
default:
log.Println("参数值类型错误", v, "not in string|int|float64|interface|int64")
}
return strings.Trim(value, " ")
}
/**
* 是否存在在字符切片中
*/
func IsInStringArray(arr []string, str string) bool {
var isIn bool = false
length := len(arr)
if length < 1 {
return false
}
for _, item := range arr {
if item == str {
isIn = true
break
}
}
return isIn
}
func ToInt(inter interface{}) int {
var value int
switch inter.(type) {
case string:
value, _ = strconv.Atoi(inter.(string))
case int:
value = inter.(int)
case int64:
value = int(inter.(int64))
case float64:
value, _ = strconv.Atoi(fmt.Sprintf("%1.0f", inter))
case nil:
value = 0
case interface{}:
value = inter.(int)
default:
log.Println("参数值类型错误", inter, "not in string|int|float64|interface|int64")
}
return value
}
func ToInt64(inter interface{}) int64 {
var value int64
switch inter.(type) {
case string:
value, _ = strconv.ParseInt(inter.(string), 10, 64)
case int:
value = int64(inter.(int))
case int64:
value = inter.(int64)
case float64:
value_int, _ := strconv.Atoi(fmt.Sprintf("%1.0f", inter))
value = int64(value_int)
case nil:
value = 0
case interface{}:
if _, ok := inter.(int64); !ok {
value = inter.(int64)
}
default:
log.Println("参数值类型错误", inter, "not in string|int|float64|interface|int64")
}
return value
}
/**
* 根据第几页计算从第几行开始
* @param pageNum 第几页
* @param pageSize 每页几行
* @return from,offset 开始行数,偏移量
* 2020/05/15
* gz
*/
func GetPage(pageNum, pageSize interface{}) (string, string) {
var from string
var offset int = ToInt(pageSize)
var pageNumInt, pageSizeInt int = ToInt(pageNum), ToInt(pageSize)
if pageNumInt < 1 {
pageNumInt = 1
}
if pageSizeInt < 1 {
offset = 10
pageSizeInt = 10
}
from = ToString((pageNumInt - 1) * pageSizeInt)
return from, ToString(offset)
}