utils

package
v3.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 40 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MM_DD                   = "MM-dd"
	YYYYMM                  = "yyyyMM"
	YYYY_MM                 = "yyyy-MM"
	YYYY_MM_DD              = "yyyy-MM-dd"
	YYYYMMDD                = "yyyyMMdd"
	YYYYMMDDHHMMSS          = "yyyyMMddHHmmss"
	YYYYMMDDHHMM            = "yyyyMMddHHmm"
	YYYYMMDDHH              = "yyyyMMddHH"
	YYMMDDHHMM              = "yyMMddHHmm"
	MM_DD_HH_MM             = "MM-dd HH:mm"
	MM_DD_HH_MM_SS          = "MM-dd HH:mm:ss"
	YYYY_MM_DD_HH_MM        = "yyyy-MM-dd HH:mm"
	YYYY_MM_DD_HH_MM_SS     = "yyyy-MM-dd HH:mm:ss"
	YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS"

	MM_DD_EN                   = "MM/dd"
	YYYY_MM_EN                 = "yyyy/MM"
	YYYY_MM_DD_EN              = "yyyy/MM/dd"
	MM_DD_HH_MM_EN             = "MM/dd HH:mm"
	MM_DD_HH_MM_SS_EN          = "MM/dd HH:mm:ss"
	YYYY_MM_DD_HH_MM_EN        = "yyyy/MM/dd HH:mm"
	YYYY_MM_DD_HH_MM_SS_EN     = "yyyy/MM/dd HH:mm:ss"
	YYYY_MM_DD_HH_MM_SS_SSS_EN = "yyyy/MM/dd HH:mm:ss.SSS"

	MM_DD_CN               = "MM月dd日"
	YYYY_MM_CN             = "yyyy年MM月"
	YYYY_MM_DD_CN          = "yyyy年MM月dd日"
	MM_DD_HH_MM_CN         = "MM月dd日 HH:mm"
	MM_DD_HH_MM_SS_CN      = "MM月dd日 HH:mm:ss"
	YYYY_MM_DD_HH_MM_CN    = "yyyy年MM月dd日 HH:mm"
	YYYY_MM_DD_HH_MM_SS_CN = "yyyy年MM月dd日 HH:mm:ss"

	HH_MM       = "HH:mm"
	HH_MM_SS    = "HH:mm:ss"
	HH_MM_SS_MS = "HH:mm:ss.SSS"
)
View Source
const KEY = "fakeKeyChangeMe!"

KEY 密钥

Variables

This section is empty.

Functions

func AesDecode

func AesDecode(encryptedString, key string) (string, error)

AesDecode aes解密

使用CFB

key应该是一个16或24或32位长度的字符

func AesEncode

func AesEncode(rawString, key string) (string, error)

AesEncode aes加密

使用CFB

key应该是一个16或24或32位长度的字符

func Base64Decode

func Base64Decode(encodedString string) ([]byte, error)

Base64Decode base64解码

func Base64Encode

func Base64Encode(rawBytes []byte) string

Base64Encode base64编码

func Crc32Checksum added in v3.0.2

func Crc32Checksum(data []byte, table *crc32.Table) uint32

Crc32Checksum 求crc32校验码

func Crc32ChecksumIEEE added in v3.0.2

func Crc32ChecksumIEEE(data []byte) uint32

Crc32ChecksumIEEE 求crc32校验码

使用IEEE多项式

func Crc64Checksum added in v3.0.2

func Crc64Checksum(data []byte, table *crc64.Table) uint64

Crc64Checksum 求crc64校验码

func Crc64ChecksumECMA added in v3.0.2

func Crc64ChecksumECMA(data []byte) uint64

Crc64ChecksumECMA 求crc64校验码

使用ECMA多项式

func FileAppendContents

func FileAppendContents(content []byte, dst string) error

FileAppendContents 将内容写入文件(追加写)

content []byte 写入的内容

dst string 写入的目标地址

func FileExists

func FileExists(dst string) bool

FileExists 检查文件上是否存在

dst string 目标地址

func FileExistsWithError

func FileExistsWithError(dst string) (bool, error)

FileExistsWithError 检查文件上是否存在(会返回错误信息)

dst string 目标地址

func FileExt

func FileExt(filename string) string

FileExt 获取文件扩展名

根据文件名最后一个.分隔来切分获取

func FileGetContents

func FileGetContents(filename string) ([]byte, error)

FileGetContents 获取文件内容

filename string 文件地址

func FileGetContentsReadLine

func FileGetContentsReadLine(dst string) (<-chan string, error)

FileGetContentsReadLine 逐行读取文件内容

func FilePutContents

func FilePutContents(content []byte, dst string) error

FilePutContents 将内容写入文件(覆盖写)

content []byte 写入的内容

dst string 写入的目标地址

func FormatDate added in v3.0.1

func FormatDate(date time.Time, dateStyle DateStyle) string

FormatDate 时间对象转字符串

func GenerateVAPIDKeys

func GenerateVAPIDKeys() (privateKey string, publicKey string, err error)

GenerateVAPIDKeys 生成web push的公私钥

func GetLocalIP

func GetLocalIP() (string, error)

GetLocalIP 获取本地ip地址(单播地址)

func ListeningExitSignal

func ListeningExitSignal(wg *sync.WaitGroup)

ListeningExitSignal 监听系统退出信号

func MD5Encrypt added in v3.0.1

func MD5Encrypt(rawString string) string

MD5Encrypt md5加密

func ParseDate added in v3.0.1

func ParseDate(date, layout string, loc *time.Location) (time.Time, error)

ParseDate 解析时间

string解析到time对象

func PrintSwaggerControllerInfo

func PrintSwaggerControllerInfo(param SwaggerControllerInfoParam) string

PrintSwaggerControllerInfo 打印swagger控制器信息

用于生成swagger注释

func PrintSwaggerSummaryInfo

func PrintSwaggerSummaryInfo(param SwaggerSummaryInfoParam) string

PrintSwaggerSummaryInfo 打印swagger概览信息参数

用于生成swagger注释

func PrintVersion added in v3.0.1

func PrintVersion(fields VersionInfoFields)

func RSADecrypt

func RSADecrypt(encodedString string, privateKey []byte) (string, error)

RSADecrypt rsa解密

func RSAEncrypt

func RSAEncrypt(rawString string, publicKey []byte) (string, error)

RSAEncrypt rsa加密

func RSASign

func RSASign(rawStringBytes, privateKey []byte) (string, error)

RSASign rsa加签

func RSAVerifySign

func RSAVerifySign(rawStringBytes, sign, publicKey []byte) (bool, error)

RSAVerifySign rsa验签

func RandomComplexString added in v3.0.2

func RandomComplexString(length int) string

RandomComplexString 随机字符串(可带特殊符号)

func RandomDigitalChars

func RandomDigitalChars(length int) string

RandomDigitalChars 随机字符串(数字)

func RandomLetters

func RandomLetters(length int) string

RandomLetters 随机字符串(字母)

func RedisClusterLock

func RedisClusterLock(key string) bool

RedisClusterLock redis锁-上锁(使用cluster)

using SetNX

func RedisClusterUnlock

func RedisClusterUnlock(key string)

RedisClusterUnlock redis锁-解锁(使用cluster)

using SetNX

func RedisStandaloneLock added in v3.0.1

func RedisStandaloneLock(key string) bool

RedisStandaloneLock redis锁-上锁(使用standalone)

using SetNX

func RedisStandaloneUnlock added in v3.0.1

func RedisStandaloneUnlock(key string)

RedisStandaloneUnlock redis锁-解锁(使用standalone)

using SetNX

func RedisTryLock added in v3.0.2

func RedisTryLock(key string) bool

RedisTryLock redis锁-尝试上锁(自动推测连接类型)

using SetNX

Note

该方法会立即返回锁定成功与否的结果

func RedisUnlock

func RedisUnlock(key string)

RedisUnlock redis锁-解锁(自动推测连接类型)

using SetNX

func SM4ECBDecrypt added in v3.0.1

func SM4ECBDecrypt(hexKey, base64Raw string) (string, error)

SM4ECBDecrypt ECB解密 @param hexKey 16进制key 长度32位 @param base64Raw 加密内容 base64格式

func SM4ECBEncrypt added in v3.0.1

func SM4ECBEncrypt(hexKey, raw string) (string, error)

SM4ECBEncrypt ECB加密 @param hexKey 16进制key 长度32位 @param raw 待加密内容

func SaveFile2Dst

func SaveFile2Dst(file *multipart.FileHeader, dst string) error

SaveFile2Dst 将文件保存到目标地址(拷贝文件)

file *multipart.FileHeader 文件

dst string 拷贝到的目标地址

func SendNotification

func SendNotification(privateKey, publicKey string, sub webpush.Subscription, subscriberEmail string, body Payload) error

SendNotification 发送通知

func StringPaddingBoth added in v3.0.2

func StringPaddingBoth(rawString, padChar string, length int) string

StringPaddingBoth 向两端填充字符串

@param rawString 原字符

@param padChar 填充字符

@param length 最终字符长度

Note

如果填充长度不能均分,那么右侧多填充一个字符,如:

rawString = "a",padChar = "#",length = 4

则:

result = "#a##"

func StringPaddingLeft added in v3.0.2

func StringPaddingLeft(rawString, padChar string, length int) string

StringPaddingLeft 向左填充字符串

@param rawString 原字符

@param padChar 填充字符

@param length 最终字符长度

func StringPaddingRight added in v3.0.2

func StringPaddingRight(rawString, padChar string, length int) string

StringPaddingRight 向右填充字符串

@param rawString 原字符

@param padChar 填充字符

@param length 最终字符长度

func StringReverse added in v3.0.2

func StringReverse(s string) string

StringReverse 翻转字符串

func StringShuffle added in v3.0.2

func StringShuffle(s string) string

StringShuffle 打乱字符串

func ValidateEmail

func ValidateEmail(email string) bool

ValidateEmail 验证邮箱格式

func ValidateIdentityCard added in v3.0.1

func ValidateIdentityCard(idCard string) bool

ValidateIdentityCard (中国大陆)身份证格式校验 计算规则参考“中国国家标准化管理委员会” 官方文档@see http://www.gb688.cn/bzgk/gb/newGbInfo?hcno=080D6FBF2BB468F9007657F26D60013E

func Wordwrap

func Wordwrap(rawStr string, length int, split string) string

Wordwrap 以给定的字符和长度来打断字符串

func WrapRedisKey

func WrapRedisKey(appName, key string) string

WrapRedisKey 包装redis键名

给redis的键加入应用名前缀,如:

appName=game key=user

最终的redis键名为:game:user

此方法的主要作用是按应用来划分redis键名

Types

type CancelFunc added in v3.0.2

type CancelFunc func()

func RedisLock

func RedisLock(key string) (cancel CancelFunc)

RedisLock redis锁-上锁(自动推测连接类型)

using SetNX

Note

该方法会阻塞住线程直到上锁成功或者调用cancel取消

type DateStyle added in v3.0.1

type DateStyle string

DateStyle 日期格式

type Payload

type Payload struct {
	Title string           `json:"title"`
	Body  string           `json:"body"`
	Icon  string           `json:"icon"`
	Data  PayloadDataField `json:"data"`
	Badge string           `json:"badge"`
}

Payload 消息推送载荷

type PayloadDataField

type PayloadDataField struct {
	URL string `json:"url"`
}

PayloadDataField 消息推送载荷-data字段

type SwaggerControllerInfoParam

type SwaggerControllerInfoParam struct {
	FunctionName       string //方法名称
	FunctionDesc       string //方法描述
	Tag                string //分类标签
	Summary            string //简要标题
	Description        string //接口描述
	Method             string //请求方法
	RequestParamString string //请求参数字符
	ResponseBodyString string //返回体字符
	ApiPath            string //接口路径
	NeedAuthorize      bool   //是否需要授权
}

SwaggerControllerInfoParam 控制器信息参数

type SwaggerSummaryInfoParam

type SwaggerSummaryInfoParam struct {
	Title            string //标题
	Version          string //版本
	Description      string //描述
	TermOfServiceUrl string //服务条款地址
	ContactName      string //联系人
	ContactUrl       string //联系网址
	ContactEmail     string //联系邮箱
	LicenseUrl       string //证书网址
	Host             string //接口主机地址
	BasePath         string //接口公共前缀路径
	NeedAuthorize    bool   //是否需要token授权
}

SwaggerSummaryInfoParam 概览信息参数

type TIM

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

func NewTimeWithTimeZone

func NewTimeWithTimeZone(timeZone ...string) *TIM

NewTimeWithTimeZone 根据时区初始化时间

默认时区: Asia/Shanghai

func (*TIM) Date

func (t *TIM) Date() string

Date 获取格式化后的当前日期

func (*TIM) Datetime

func (t *TIM) Datetime() string

Datetime 获取格式化后的当前日期时间

func (*TIM) Now

func (t *TIM) Now() time.Time

Now 获取当前时间对象(带时区)

func (*TIM) Time

func (t *TIM) Time() string

Time 获取格式化后的当前时间

type VersionInfoFields added in v3.0.1

type VersionInfoFields struct {
	AppName   string //应用名称
	Branch    string //版本分支
	Version   string //版本号
	Revision  string //commit id
	BuildDate string //编译时间
	GoVersion string //golang版本
}

VersionInfoFields 版本信息字段

Jump to

Keyboard shortcuts

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