parent
6c3420e9ab
commit
17e61153b0
@ -1,2 +1,2 @@
|
||||
github.com/nilorg/sdk v0.0.0-20210429091026-95b6cdc95c84 h1:Nxk1uViXfb9MHgtHBlQFWzlQCsJbDQuotfTsAFcFP3o=
|
||||
github.com/nilorg/sdk v0.0.0-20210429091026-95b6cdc95c84/go.mod h1:X1swpPdqguAZaBDoEPyEWHSsJii0YQ1o+3piMv6W3JU=
|
||||
github.com/dtapps/gostring v1.0.1 h1:J04kndQ08LOqb+H41s8PbMEgNtpheV9iTTl7DSPIZVk=
|
||||
github.com/dtapps/gostring v1.0.1/go.mod h1:BYYnZHrmwpFXkLpd9rQyV5YcChovVreacfcjyMKEwoU=
|
||||
|
@ -0,0 +1,8 @@
|
||||
.env
|
||||
.git
|
||||
.svn
|
||||
.idea
|
||||
.vscode
|
||||
*.log
|
||||
gitmod.sh
|
||||
*_test.go
|
2
vendor/github.com/nilorg/sdk/LICENSE → vendor/github.com/dtapps/gostring/LICENSE
generated
vendored
2
vendor/github.com/nilorg/sdk/LICENSE → vendor/github.com/dtapps/gostring/LICENSE
generated
vendored
@ -1,6 +1,6 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Nil Org
|
||||
Copyright (c) 2018 茂名聚合科技有限公司
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
@ -0,0 +1,25 @@
|
||||
<h1>
|
||||
<a href="https://www.dtapp.net/">Golang String</a>
|
||||
</h1>
|
||||
|
||||
📦 Golang 字符串组件
|
||||
|
||||
[comment]: <> (go)
|
||||
[![godoc](https://pkg.go.dev/badge/github.com/dtapps/gostring?status.svg)](https://pkg.go.dev/github.com/dtapps/gostring)
|
||||
[![goproxy.cn](https://goproxy.cn/stats/github.com/dtapps/gostring/badges/download-count.svg)](https://goproxy.cn/stats/github.com/dtapps/gostring)
|
||||
[![goreportcard.com](https://goreportcard.com/badge/github.com/dtapps/gostring)](https://goreportcard.com/report/github.com/dtapps/gostring)
|
||||
[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/github.com%2Fdtapps%2Fgostring)
|
||||
|
||||
#### 安装使用
|
||||
|
||||
```go
|
||||
go get -v -u github.com/dtapps/gostring
|
||||
```
|
||||
|
||||
#### 导入
|
||||
|
||||
```go
|
||||
import (
|
||||
"github.com/dtapps/gostring"
|
||||
)
|
||||
```
|
@ -0,0 +1,151 @@
|
||||
package gostring
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const Version = "1.0.1"
|
||||
|
||||
// ToString 转换成string
|
||||
func ToString(value interface{}) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprint(value)
|
||||
}
|
||||
|
||||
// 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, err := strconv.ParseInt(s, 10, 64)
|
||||
if err == nil {
|
||||
return i
|
||||
}
|
||||
return int64(ToFloat64(s))
|
||||
}
|
||||
|
||||
// ToUint string到uint64
|
||||
func ToUint(s string) uint {
|
||||
i, err := strconv.ParseUint(s, 10, 64)
|
||||
if err == nil {
|
||||
return uint(i)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// ToUint64 string到uint64
|
||||
func ToUint64(s string) uint64 {
|
||||
i, err := strconv.ParseUint(s, 10, 64)
|
||||
if err == nil {
|
||||
return i
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// Contains 判断字符串是否包含某个字符
|
||||
func Contains(s, substr string) bool {
|
||||
return strings.Contains(s, substr)
|
||||
}
|
||||
|
||||
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(intVal, 10)
|
||||
case float32:
|
||||
floatVal, _ := value.(float32)
|
||||
val = strconv.FormatFloat(float64(floatVal), 'f', -1, 32)
|
||||
case float64:
|
||||
floatVal, _ := value.(float64)
|
||||
val = strconv.FormatFloat(floatVal, 'f', -1, 64)
|
||||
}
|
||||
return val, true
|
||||
}
|
@ -1,112 +0,0 @@
|
||||
package convert
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// ToString 转换成string
|
||||
func ToString(value interface{}) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprint(value)
|
||||
}
|
||||
|
||||
// ToBool 转换成Bool
|
||||
func ToBool(i interface{}) bool {
|
||||
switch b := i.(type) {
|
||||
case bool:
|
||||
return b
|
||||
case nil:
|
||||
return false
|
||||
case int:
|
||||
if i.(int) != 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
case string:
|
||||
v, err := strconv.ParseBool(ToString(i))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return v
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// ToInt 转换成int
|
||||
func ToInt(value interface{}) int {
|
||||
return int(ToInt64(value))
|
||||
}
|
||||
|
||||
// ToInt32 转换成int32
|
||||
func ToInt32(value interface{}) int32 {
|
||||
return int32(ToInt64(value))
|
||||
}
|
||||
|
||||
// ToInt64 转换成int64
|
||||
func ToInt64(value interface{}) int64 {
|
||||
num, err := strconv.ParseInt(ToString(value), 10, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
// ToUint 转换成uint
|
||||
func ToUint(value interface{}) uint {
|
||||
return uint(ToUint64(value))
|
||||
}
|
||||
|
||||
// ToUint32 转换成uint32
|
||||
func ToUint32(value interface{}) uint32 {
|
||||
return uint32(ToUint64(value))
|
||||
}
|
||||
|
||||
// ToUint64 转换成uint64
|
||||
func ToUint64(value interface{}) uint64 {
|
||||
num, err := strconv.ParseUint(ToString(value), 10, 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
// ToFloat32 转换成float32
|
||||
func ToFloat32(value interface{}) float32 {
|
||||
return float32(ToFloat64(value))
|
||||
}
|
||||
|
||||
// ToFloat64 转换成float64
|
||||
func ToFloat64(value interface{}) float64 {
|
||||
num, err := strconv.ParseFloat(ToString(value), 64)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
// BytesToInt32 字节转Int32
|
||||
func BytesToInt32(data []byte) int32 {
|
||||
var num int32
|
||||
buffer := bytes.NewBuffer(data)
|
||||
binary.Read(buffer, binary.BigEndian, &num)
|
||||
return num
|
||||
}
|
||||
|
||||
// BytesToInt 字节转Int
|
||||
func BytesToInt(data []byte) int {
|
||||
return int(BytesToInt32(data))
|
||||
}
|
||||
|
||||
// BytesToInt64 字节转Int64
|
||||
func BytesToInt64(data []byte) int64 {
|
||||
var num int64
|
||||
buffer := bytes.NewBuffer(data)
|
||||
binary.Read(buffer, binary.BigEndian, &num)
|
||||
return num
|
||||
}
|
@ -1,3 +1,3 @@
|
||||
# github.com/nilorg/sdk v0.0.0-20210429091026-95b6cdc95c84
|
||||
## explicit; go 1.12
|
||||
github.com/nilorg/sdk/convert
|
||||
# github.com/dtapps/gostring v1.0.1
|
||||
## explicit
|
||||
github.com/dtapps/gostring
|
||||
|
Loading…
Reference in new issue