parent
b506aab9ef
commit
d48e235d44
@ -0,0 +1,41 @@
|
||||
package gojson
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func Encode(v interface{}) (string, error) {
|
||||
bytes, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
func MarshalToString(msg interface{}) (string, error) {
|
||||
j, e := json.Marshal(msg)
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
return string(j), nil
|
||||
}
|
||||
|
||||
func JsonDecode(data string) (map[string]interface{}, error) {
|
||||
var dat map[string]interface{}
|
||||
err := json.Unmarshal([]byte(data), &dat)
|
||||
return dat, err
|
||||
}
|
||||
|
||||
func JsonDecodeNoError(data string) map[string]interface{} {
|
||||
var dat map[string]interface{}
|
||||
_ = json.Unmarshal([]byte(data), &dat)
|
||||
return dat
|
||||
}
|
||||
|
||||
func JsonEncode(data interface{}) (string, error) {
|
||||
jsons, err := json.Marshal(data)
|
||||
return string(jsons), err
|
||||
}
|
||||
|
||||
func JsonEncodeNoError(data interface{}) string {
|
||||
jsons, _ := json.Marshal(data)
|
||||
return string(jsons)
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package random
|
||||
package gorandom
|
||||
|
||||
import (
|
||||
"math/rand"
|
@ -1,4 +1,4 @@
|
||||
package random
|
||||
package gorandom
|
||||
|
||||
import (
|
||||
"fmt"
|
@ -0,0 +1,114 @@
|
||||
package gostring
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// ToFloat64 string到float64
|
||||
func ToFloat64(s string) float64 {
|
||||
i, _ := strconv.ParseFloat(s, 64)
|
||||
return i
|
||||
}
|
||||
|
||||
// ToInt string到int
|
||||
func ToInt(s string) int {
|
||||
i, _ := strconv.Atoi(s)
|
||||
return i
|
||||
}
|
||||
|
||||
// ToInt64 string到int64
|
||||
func ToInt64(s string) int64 {
|
||||
i, _ := strconv.ParseInt(s, 10, 64)
|
||||
return i
|
||||
}
|
||||
|
||||
// Replace 字符串替换
|
||||
func Replace(str, old, new string) string {
|
||||
return strings.Replace(str, old, new, -1)
|
||||
}
|
||||
|
||||
func HmacSha256Hex(key, strToSign string) string {
|
||||
hasHer := hmac.New(sha256.New, []byte(key))
|
||||
hasHer.Write([]byte(strToSign))
|
||||
return hex.EncodeToString(hasHer.Sum(nil))
|
||||
}
|
||||
|
||||
// Space 去除空格
|
||||
func Space(str string) string {
|
||||
return strings.Replace(str, " ", "", -1)
|
||||
}
|
||||
|
||||
// LineBreak 去除换行符
|
||||
func LineBreak(str string) string {
|
||||
return strings.Replace(str, "\n", "", -1)
|
||||
}
|
||||
|
||||
// SpaceAndLineBreak 去除空格和去除换行符
|
||||
func SpaceAndLineBreak(str string) string {
|
||||
return LineBreak(Space(str))
|
||||
}
|
||||
|
||||
// TrimLastChar 删除字符串中的最后一个
|
||||
func TrimLastChar(s string) string {
|
||||
r, size := utf8.DecodeLastRuneInString(s)
|
||||
if r == utf8.RuneError && (size == 0 || size == 1) {
|
||||
size = 0
|
||||
}
|
||||
return s[:len(s)-size]
|
||||
}
|
||||
|
||||
// Split 字符串分隔
|
||||
func Split(s string, sep string) []string {
|
||||
return strings.Split(s, sep)
|
||||
}
|
||||
|
||||
func NumericalToString(value interface{}) (string, bool) {
|
||||
var val string
|
||||
|
||||
switch value.(type) {
|
||||
default:
|
||||
return "0", false
|
||||
case int:
|
||||
intVal, _ := value.(int)
|
||||
val = strconv.FormatInt(int64(intVal), 10)
|
||||
case int8:
|
||||
intVal, _ := value.(int8)
|
||||
val = strconv.FormatInt(int64(intVal), 10)
|
||||
case int16:
|
||||
intVal, _ := value.(int16)
|
||||
val = strconv.FormatInt(int64(intVal), 10)
|
||||
case int32:
|
||||
intVal, _ := value.(int32)
|
||||
val = strconv.FormatInt(int64(intVal), 10)
|
||||
case int64:
|
||||
intVal, _ := value.(int64)
|
||||
val = strconv.FormatInt(int64(intVal), 10)
|
||||
case uint:
|
||||
intVal, _ := value.(uint)
|
||||
val = strconv.FormatUint(uint64(intVal), 10)
|
||||
case uint8:
|
||||
intVal, _ := value.(uint8)
|
||||
val = strconv.FormatUint(uint64(intVal), 10)
|
||||
case uint16:
|
||||
intVal, _ := value.(uint16)
|
||||
val = strconv.FormatUint(uint64(intVal), 10)
|
||||
case uint32:
|
||||
intVal, _ := value.(uint32)
|
||||
val = strconv.FormatUint(uint64(intVal), 10)
|
||||
case uint64:
|
||||
intVal, _ := value.(uint64)
|
||||
val = strconv.FormatUint(uint64(intVal), 10)
|
||||
case float32:
|
||||
floatVal, _ := value.(float32)
|
||||
val = strconv.FormatFloat(float64(floatVal), 'f', -1, 32)
|
||||
case float64:
|
||||
floatVal, _ := value.(float64)
|
||||
val = strconv.FormatFloat(float64(floatVal), 'f', -1, 64)
|
||||
}
|
||||
return val, true
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package gostring
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
str := "iPhone 11 Pro Max<iPhone12,5>"
|
||||
fmt.Printf("%d\n", strings.LastIndex(str, "<"))
|
||||
fmt.Printf("%d\n", strings.LastIndex(str, "("))
|
||||
fmt.Printf("%d\n", strings.LastIndex("iPad (6th generation, WiFi)<iPad7,5>", "<"))
|
||||
fmt.Printf("%d\n", strings.LastIndex("iPad (6th generation, WiFi)<iPad7,5>", "("))
|
||||
s := str[0:17]
|
||||
fmt.Printf("%s\n", s)
|
||||
str = "iPad (6th generation, WiFi)<iPad7,5>"
|
||||
s = str[0:5]
|
||||
fmt.Printf("%s\n", s)
|
||||
fmt.Printf(strings.TrimSpace(s))
|
||||
}
|
||||
|
||||
func TestToInt64(t *testing.T) {
|
||||
log.Println(ToInt64("120"))
|
||||
log.Println(ToInt64("120.9"))
|
||||
}
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
str := "wx6566ef69e8738ad9"
|
||||
fmt.Println(strings.Contains(str, "wx"))
|
||||
myString := "www.5lmh.com"
|
||||
if strings.HasPrefix(myString, "www") {
|
||||
fmt.Println("Hello to you too")
|
||||
} else {
|
||||
fmt.Println("Goodbye")
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package json
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
func Encode(v interface{}) (string, error) {
|
||||
bytes, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(bytes), nil
|
||||
}
|
||||
|
||||
func MarshalToString(msg interface{}) (string, error) {
|
||||
j, e := json.Marshal(msg)
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
return string(j), nil
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package string
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func HmacSha256Hex(key, strToSign string) string {
|
||||
hasher := hmac.New(sha256.New, []byte(key))
|
||||
hasher.Write([]byte(strToSign))
|
||||
return hex.EncodeToString(hasher.Sum(nil))
|
||||
}
|
||||
|
||||
func ToLower(str string) string {
|
||||
return strings.ToLower(str)
|
||||
}
|
||||
|
||||
func ToUpper(str string) string {
|
||||
return strings.ToUpper(str)
|
||||
}
|
Loading…
Reference in new issue