util

package
v0.0.0-...-c4769b5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2020 License: MIT Imports: 34 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KC_RAND_KIND_NUM   = 0 // 纯数字
	KC_RAND_KIND_LOWER = 1 // 小写字母
	KC_RAND_KIND_UPPER = 2 // 大写字母
	KC_RAND_KIND_ALL   = 3 // 数字、大小写字母
)
View Source
const (
	STR_PAD_LEFT int = iota
	STR_PAD_RIGHT
	STR_PAD_BOTH
)
View Source
const (
	// QUERY_RFC1738 则编码将会以 » RFC 1738 标准和 application/x-www-form-urlencoded 媒体类型进行编码,空格会被编码成加号(+)。
	QUERY_RFC1738 int = iota
	// QUERY_RFC3986 将根据 » RFC 3986 编码,空格会被百分号编码(%20)。
	QUERY_RFC3986
)

Variables

This section is empty.

Functions

func AesDecrypt

func AesDecrypt(data, iv, key []byte) ([]byte, error)

AesDecrypt aes解密

func AesEncrypt

func AesEncrypt(data, iv, key []byte) ([]byte, error)

AesEncrypt aes加密

func CamelCase

func CamelCase(s string) string

CamelCase 将一个字符串转为大驼峰命名

func CamelCaseInitialism

func CamelCaseInitialism(s string) string

CamelCaseInitialism 将一个字符串转为大驼峰命名,强制首字母缩写命名规范

func Command

func Command(bin string, argv []string, baseDir string) ([]byte, error)

Command execute system cmd

func Concat

func Concat(args ...string) string

Concat 连接字符串

func CopyFile

func CopyFile(s, d string) error

CopyFile copy source file to destination file

func DownloadFile

func DownloadFile(uri string, file string) (int, error)

DownloadFile 下载文件

func Empty

func Empty(v interface{}) bool

Empty ...

func Eq

func Eq(v1, v2 interface{}) bool

Eq ...

func FormatSize

func FormatSize(s int64) string

FormatSize 格式化size单位 输出友好格式(B,KB,MB,GB,TB)

func FriendlyTime

func FriendlyTime(t time.Time) string

FriendlyTime 将时间格式化为: 几秒前,几分钟前,几小时前,日期 2006-01-02

func GetDir

func GetDir() string

GetDir get pwd

func GetExt

func GetExt(file string) string

GetExt get file extenstion, not contains dot

func GetPathSize

func GetPathSize(path string) (int64, error)

GetPathSize 获取一个文件或目录的大小

func Glob

func Glob(base, pattern string, abs bool) ([]string, error)

Glob 查找指定目录下的文件列表

func Gt

func Gt(v1, v2 int64) bool

Gt ...

func HTTPBuildQuery

func HTTPBuildQuery(values map[string]interface{}, encType int) string

HTTPBuildQuery 模仿php的http_build_query构建字符串 JAVA String resultMD5 = MD5.md5(result.toString().replace("*", "%2A").replace("%7E", "~").replace("+", "%20"));

func HttpGet

func HttpGet(url string, timeout time.Duration) ([]byte, error)

func IP2Long

func IP2Long(ipstr string) uint32

IP2Long ip to int

func InSlice

func InSlice(si interface{}, s interface{}, t string) bool

InSlice 判断元素s是否在slice si中出现过,返回 bool

func Int2String

func Int2String(i int) string

Int2String format int to string

func IntToString

func IntToString(i int) string

IntToString 将数字转换为字符串

func IsExist

func IsExist(path string) bool

IsExist check path is exists, exist return true, not exist return false

func IsNumeric

func IsNumeric(s string) bool

IsNumeric 判断是否是纯数字,空字符串不是数字,0才是

func IsWritable

func IsWritable(path string) bool

IsWritable check path is writeable, can return true, can not return false

func KeepTags

func KeepTags(s []byte, tags ...string) []byte

KeepTags 过滤HTML标签,只保留 指定的tag

func LocalIPAddrs

func LocalIPAddrs() []string

LocalIPAddrs get local public address

func Long2IP

func Long2IP(ipLong uint32) string

Long2IP int to ip

func Lt

func Lt(v1, v2 int64) bool

Lt ...

func MD5

func MD5(s string) string

MD5 get hash of string

func MD5Bytes

func MD5Bytes(s []byte) string

MD5Bytes get hash of bytes

func MD5File

func MD5File(filename string) string

MD5File get hash of file content

func MapFillStruct

func MapFillStruct(data map[string]interface{}, obj interface{}) error

MapFillStruct 用map填充结构体,遇到未知字段或数值转换错误返回具体错误

func MapFillStructMust

func MapFillStructMust(data map[string]interface{}, obj interface{})

MapFillStructMust 用map填充结构体,忽略所有不能填充的类型

func MathRound

func MathRound(x float64) int

MathRound 四舍五入一个浮点数,返回整数

func MixedToSliceInt

func MixedToSliceInt(v interface{}) []int

MixedToSliceInt 将混合类型转为 数字slice

func MixedToSliceString

func MixedToSliceString(v interface{}) []string

MixedToSliceString 将混合类型转为 字符串slice

func MkdirAll

func MkdirAll(path string) error

MkdirAll check the path isexist or mkdir, and check writable

func Neq

func Neq(v1, v2 interface{}) bool

Neq ...

func Nl2br

func Nl2br(text string) template.HTML

Nl2br 将换行转换成HTML中的BR

func PKCS5Padding

func PKCS5Padding(data []byte, blockSize int) []byte

PKCS5Padding aes加密补码

func PKCS5UnPadding

func PKCS5UnPadding(data []byte, blockSize int) ([]byte, error)

PKCS5UnPadding aes解密去码

func RandFileName

func RandFileName() string

RandFileName 随机生成一个文件名

func RandInt

func RandInt(max int) int

RandInt 随机数字

func RandStr

func RandStr(size int, kind int) []byte

随机字符串

func ReadDir

func ReadDir(dirname string) ([]os.FileInfo, error)

ReadDir read path return files by os.FileInfo

func ReadFile

func ReadFile(file string) ([]byte, error)

ReadFile read file content

func Scale

func Scale(src, dst string, w, h int, equalRate bool, cut bool) error

Scale 缩放图像 参数:src 原始图像路径,dst 目标图像路径,如果为空或相同则覆盖原始文件,w,h 为缩放后的宽高 equalRate 参数决定是否等比缩放,开启等比缩放时,将大小控制在指定的区域内,图像尺寸小于等于给定尺寸 cut 参数觉决定是否使用裁剪方案,当使用裁剪时,使用较小的缩放比,满足最小边在指定区域内,同时将多余(在区域外)的部分减掉 当使用 cut 启用裁剪时,忽略 equalRate的设置,使用等比缩放并裁剪 缩放函数有四个 draw.NearestNeighbor, draw.ApproxBiLinear, draw.BiLinear, draw.CatmullRom, 具体区别参见:https://godoc.org/github.com/golang/image/draw#pkg-variables// 使用方法,非常简单:

err = Scale("2.jpg", "a1.png", 100, 100, false, false)

err = Scale("2.jpg", "a2.png", 100, 100, true, false) err = Scale("2.jpg", "a3.png", 100, 100, false, true) err = Scale("2.jpg", "a4.png", 100, 100, true, true)

func SecondToTime

func SecondToTime(second int) string

SecondToTime 将秒格式化为 几小时:几分钟:几秒

func SliceIntDiff

func SliceIntDiff(a []int, b []int) []int

SliceIntDiff 获取在数字切片 a 中但不在数字切片 b 中的差集

func SliceIntEqual

func SliceIntEqual(a, b []int) bool

SliceIntEqual 判断两个数字切片中的内容是否相同,忽略元素的排序 [1,2,3] == [3,2,1]

func SliceIntRand

func SliceIntRand(a []int) []int

SliceIntRand 将切片顺序打乱随机返回

func SliceIntToString

func SliceIntToString(a []int) []string

SliceIntToString 将数字切片转换为字符串切片 [11,22,33] 转换为 ["11","22","33"]

func SliceStringDiff

func SliceStringDiff(a []string, b []string) []string

SliceStringDiff 获取在字符串切片 a 中但不在字符串切片 b 中的差集

func SortByID

func SortByID(sortIDs []int, in interface{}, field string) interface{}

SortByID 对结构体Slice的field字段参照sortIDs顺序排序

func StrNatCut

func StrNatCut(s string, length int, dot ...string) string

StrNatCut 字符串截取,中文算一个 英文算两个

func StrPad

func StrPad(v interface{}, length int, pad string, padType int) string

StrPad 使用另一个字符串填充字符串为指定长度

func String2Int

func String2Int(s string) int

String2Int format string to int

func StringToInt

func StringToInt(s string) int

StringToInt 将字符串转为int

func StringToInt64

func StringToInt64(s string) int64

StringToInt64 将字符串转为int64

func StripTags

func StripTags(s []byte, tags ...string) []byte

StripTags 过滤HTML标签,只去除 指定的tag

func StructToMap

func StructToMap(v interface{}, data map[string]interface{})

StructToMap 将结构体的字段填充到map中

func StructToStruct

func StructToStruct(src, dst interface{})

StructToStruct 结构体COPY

func ToString

func ToString(v interface{}) string

ToString 将任意一个类型转换为字符串

func Validate

func Validate(str, category string) bool

func VersionDiff

func VersionDiff(v1, v2 string) bool

VersionDiff 如果v1大于等于v2返回true, 否则返回 false

func WriteFile

func WriteFile(file string, content []byte) (int, error)

WriteFile write file content

Types

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error 通用错误

func Errorf

func Errorf(format string, v ...interface{}) *Error

Errorf 报错

func NewError

func NewError(code int, message interface{}) *Error

NewError 创建新的错误

func (Error) Error

func (e Error) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL