utils

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: May 20, 2025 License: GPL-3.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Invalid   = Variant(iota) // Invalid UUID
	RFC4122                   // The variant specified in RFC4122
	Reserved                  // Reserved, NCS backward compatibility.
	Microsoft                 // Reserved, Microsoft Corporation backward compatibility.
	Future                    // Reserved for future definition.
)

Constants returned by Variant.

Variables

This section is empty.

Functions

func CompareIntSlice

func CompareIntSlice(a, b []int) bool

CompareIntSlice 比较两个int型切片是否相等, true:相等, false:不相等

func CreateDir

func CreateDir(dirPath string) (err error)

CreateDir 创建目录

func DateStringToIntYear

func DateStringToIntYear(dateStr string) int

获取int型的年份

func DateStringToTime

func DateStringToTime(dateStr string) (t time.Time)

将时期字符串转日期整型时间

func DirIsExist

func DirIsExist(dirPath string) bool

DirIsExist 判断目录是否存在

func ExtractCnChar

func ExtractCnChar(content string) (result string)

提取全部中文字符

func ExtractEnChar

func ExtractEnChar(content string) (result string)

提取全部英文字符

func ExtractNameChar

func ExtractNameChar(content string) (result string)

提取姓名

func ExtractNumChar

func ExtractNumChar(content string) (result string)

提取全部数字

func ExtractNumEnCnChar

func ExtractNumEnCnChar(content string) (result string)

提取全部英文数字中文字符

func Fen2YuanInt64

func Fen2YuanInt64(price int64) int64

分转元

func Fen2YuanString

func Fen2YuanString(price int64) string

分转元

func FileIsExist

func FileIsExist(filePath string) bool

FileIsExist 判断文件是否存在 存在返回 true 不存在返回false

func Float64Yuan2FenInt64

func Float64Yuan2FenInt64(price float64) int64

元转分,乘以100后,保留整数部分

func GenerateUUID

func GenerateUUID() string

func GetIntMonth

func GetIntMonth() int

获取int型的月份

func GetIntYear

func GetIntYear() int

获取int型的年份

func GetIp

func GetIp() (ip string)

func If

func If[K comparable](cond bool, Then K, Else K) K

func InitLocalCache

func InitLocalCache(maxSizeMB int)

func Int64ToDateString

func Int64ToDateString(timeUnix int64) string

func Int64ToTimeString

func Int64ToTimeString(timeUnix int64) string

func InterfaceToDuration

func InterfaceToDuration(data interface{}) time.Duration

func InterfaceToInt64

func InterfaceToInt64(data interface{}) int64

func InterfaceToString

func InterfaceToString(data interface{}) string

func IsNum

func IsNum(s string) bool

判断字符串是否为数字

func IsRecordNotFoundError

func IsRecordNotFoundError(err error) bool

func IsSliceInt

func IsSliceInt(val int, slice []int) bool

func IsSliceInt64

func IsSliceInt64(val int64, slice []int64) bool

值是否存在

func IsSliceString

func IsSliceString(val string, slice []string) bool

值是否存在

func MD5

func MD5(str string) string

func PageNum

func PageNum(total, pageSize int) (pageNum int)

获取分页数量

func Random

func Random(min, max int) int

func ReadBigFile

func ReadBigFile(filePath string, handle func(string)) error

ReadBigFile 大文件读取 适合:常规带换行符,单行没有超过4069个字节的

func ReadBinaryFile

func ReadBinaryFile(filePath string, handle func(string)) error

ReadBinaryFile 大文件读取 适合:读取二进制文件,如音频视频流

func ReadFile

func ReadFile(filePath string) (content []byte)

ReadFile 普通小文件全量读取

func ReadLine

func ReadLine(filePath string) (result []string)

* * 一行一行的读文件

func ReadLine(filePth string, result func([]byte)) error {

func RemoveReplicaSliceInt

func RemoveReplicaSliceInt(slc []int) []int

* * slice(int类型)元素去重

func RemoveReplicaSliceString

func RemoveReplicaSliceString(slc []string) []string

* * slice(string类型)元素去重

func Sha1

func Sha1(str string) string

func SliceStringToString

func SliceStringToString(content []string) (result string)

slice字符串转json

func StringToSliceString

func StringToSliceString(content string) (result []string)

json字符串转slice

func StringToTime

func StringToTime(timeStr string) (t time.Time)

将时间字符串转time.time

func StringYuan2FenInt64

func StringYuan2FenInt64(price string) int64

元转分,乘以100后,保留整数部分

func StructSort

func StructSort(list []interface{}, field string, order string)

按照字段排序,需要注意是否有该字段和字段类型

func TimeFormat

func TimeFormat(t time.Time) string

将time.time转换时间字符串

func Utf8StrCut

func Utf8StrCut(content string, length int) string

Utf8StrCut 中文字符切取

func VerifyMobileFormat

func VerifyMobileFormat(mobileNum string) bool

mobile verify

func WriteFile

func WriteFile(filePath, content string, append bool) bool

WriteFile 文件内容写入

Types

type Bucket

type Bucket struct {
	Slice []interface{}                //承载以任意结构体为元素构成的Slice
	By    func(p, q *interface{}) bool //排序规则函数,当需要对新的结构体slice进行排序时,只需定义这个函数即可
}

通用结构体排序 结构体排序,必须重写数组Len() Swap() Less()函数

func (Bucket) Len

func (this Bucket) Len() int

数组长度Len()

func (Bucket) Less

func (this Bucket) Less(i, j int) bool

比较函数,使用外部传入的by比较函数

func (Bucket) Swap

func (this Bucket) Swap(i, j int)

元素交换

type LocalCache

type LocalCache struct {
	CacheSize int
	// contains filtered or unexported fields
}

LocalCache 使用了freecache、支持缓存淘汰、线程安全

var LocalCacheInstance LocalCache

func (*LocalCache) Del

func (l *LocalCache) Del(key string)

func (*LocalCache) Get

func (l *LocalCache) Get(key string) string

func (*LocalCache) Set

func (l *LocalCache) Set(key, value string, expire int)

type Set

type Set[K comparable] struct {
	// contains filtered or unexported fields
}

Set 不支持并发操作的Set

func NewSet

func NewSet[K comparable](items ...K) *Set[K]

func (*Set[K]) Add

func (s *Set[K]) Add(member ...K)

func (*Set[K]) Has

func (s *Set[K]) Has(member K) bool

func (*Set[K]) List

func (s *Set[K]) List() []K

type UUID

type UUID [16]byte

A UUID is a 128 bit (16 byte) Universal Unique IDentifier as defined in RFC 4122.

func Must

func Must(uuid UUID, err error) UUID

Must returns uuid if err is nil and panics otherwise.

func NewRandom

func NewRandom() (UUID, error)

NewRandom returns a Random (Version 4) UUID.

The strength of the UUIDs is based on the strength of the crypto/rand package.

Uses the randomness pool if it was enabled with EnableRandPool.

A note about uniqueness derived from the UUID Wikipedia entry:

Randomly generated UUIDs have 122 random bits.  One's annual risk of being
hit by a meteorite is estimated to be one chance in 17 billion, that
means the probability is about 0.00000000006 (6 × 10−11),
equivalent to the odds of creating a few tens of trillions of UUIDs in a
year and having one duplicate.

func (UUID) String

func (uuid UUID) String() string

String returns the string form of uuid, xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx , or "" if uuid is invalid.

func (UUID) URN

func (uuid UUID) URN() string

URN returns the RFC 2141 URN form of uuid, urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, or "" if uuid is invalid.

func (UUID) Variant

func (uuid UUID) Variant() Variant

Variant returns the variant encoded in uuid.

func (UUID) Version

func (uuid UUID) Version() Version

Version returns the version of uuid.

type Variant

type Variant byte

A Variant represents a UUID's variant.

func (Variant) String

func (v Variant) String() string

type Version

type Version byte

A Version represents a UUID's version.

func (Version) String

func (v Version) String() string

Jump to

Keyboard shortcuts

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