utilz

package module
v0.0.0-...-8bb4de0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2022 License: MIT Imports: 35 Imported by: 5

README

utilz

Golang utility library

Documentation

Index

Constants

View Source
const (
	DateFormat = "2006-01-02"
	TimeFormat = "2006-01-02 15:04:05"
)

Variables

View Source
var ErrFormatNotSupport = errors.New("format not support")
View Source
var MemCacheProvidor *memCacheProvidor

Functions

func AesDecode

func AesDecode(key, cipherText []byte) ([]byte, error)

func AesEncode

func AesEncode(key, plainText []byte) ([]byte, error)

func AsyncDo

func AsyncDo[K any](ctx context.Context, inputs []K,
	fn func(context.Context, K) error) error

func AsyncResult

func AsyncResult[K, V any](ctx context.Context, inputs []K,
	fn func(context.Context, K) (V, error)) ([]V, error)

func Base64Decode

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

func Base64Encode

func Base64Encode(data []byte) string

func ByteSizeString

func ByteSizeString(b int64) string

func CRC32

func CRC32(data []byte) uint32

CRC32

func CRC32File

func CRC32File(path string) (uint32, error)

func CRC32String

func CRC32String(s string) uint32

func CacheGet

func CacheGet(ctx context.Context, providor CacheProvidor, key string, expire time.Duration, callback FetchFn) ([]byte, error)

func CacheHash

func CacheHash(data any) string

func CeilDiv

func CeilDiv[T Integer](x, y T) T

func CheckPasswordHash

func CheckPasswordHash(password, hash string) bool

func CompileRegexp

func CompileRegexp(exp string) *regexp.Regexp

func Contains

func Contains[T comparable](slice []T, element T) bool

func CopyFile

func CopyFile(src, dst string) error

func Download

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

func DownloadFile

func DownloadFile(url, path string, timeout time.Duration) error

func EscapeSQLString

func EscapeSQLString(sql string) string

func Exec

func Exec(command string, args []string, stdin []byte) ([]byte, error)

func FileExist

func FileExist(path string) bool

func Filter

func Filter[V any](slice []V, fn func(V) bool) []V

func Fnv1

func Fnv1(data []byte) uint64

Fnv1

func Fnv1String

func Fnv1String(s string) uint64

func FormatDate

func FormatDate(t time.Time) string

func FormatTime

func FormatTime(t time.Time) string

func FromJSON

func FromJSON(data []byte, v any) error

func FromJSONString

func FromJSONString(data string, v any) error

func GetEnvBool

func GetEnvBool(name string) bool

func GetEnvString

func GetEnvString(name string) string

func GetGithubLastestRelease

func GetGithubLastestRelease(owner, repo, filename string) (tag, url string, err error)

func HttpAdvancedGet

func HttpAdvancedGet(url string, header map[string]string, timeout time.Duration) ([]byte, error)

func HttpAdvancedPost

func HttpAdvancedPost(url string, header map[string]string, body []byte, timeout time.Duration) ([]byte, error)

func HttpGet

func HttpGet(url string) ([]byte, error)

func HttpPost

func HttpPost(url string, body []byte) ([]byte, error)

func HttpPostJSON

func HttpPostJSON(url string, data any) ([]byte, error)

func If

func If[T any](condition bool, ifOutput T, elseOutput T) T

func Keys

func Keys[K comparable, V any](m map[K]V) []K

func LoadEnvFile

func LoadEnvFile(filename string) error

func MD5

func MD5(data []byte) string

MD5

func MD5File

func MD5File(path string) (string, error)

func MD5String

func MD5String(s string) string

func Main

func Main(fn func() error)

func Map

func Map[T any, R any](slice []T, fn func(T) R) []R

func Max

func Max[T Number](nums ...T) T

func Min

func Min[T Number](nums ...T) T

func Now

func Now() string

func PageCount

func PageCount[T Integer](totalRows, pageSize T) T

func PageOffset

func PageOffset[T Integer](pageNum, pageSize T) T

func ParseTime

func ParseTime(s string) (time.Time, error)

func PasswordHash

func PasswordHash(password string) (string, error)

Password

func Pkcs7Pad

func Pkcs7Pad(buf []byte, size int) ([]byte, error)

func Pkcs7Unpad

func Pkcs7Unpad(buf []byte) ([]byte, error)

func RandBytes

func RandBytes(length int) []byte

func RandInt

func RandInt(n int) int

func RandRange

func RandRange(min, max int) int

func RandString

func RandString(length int) string

func ReadFileLineByLine

func ReadFileLineByLine(path string) ([]string, error)

func ReadFileToString

func ReadFileToString(path string) (string, error)

func RegexpFindString

func RegexpFindString(exp, data string) [][]string

func SHA1

func SHA1(data []byte) string

SHA1

func SHA1File

func SHA1File(path string) (string, error)

func SHA1String

func SHA1String(s string) string

func SHA256

func SHA256(data []byte) string

SHA256

func SHA256File

func SHA256File(path string) (string, error)

func SHA256String

func SHA256String(s string) string

func SignECDSA

func SignECDSA(r io.Reader, privKey []byte) (string, error)

func StringToBool

func StringToBool(s string) bool

func Time

func Time() int64

func TimeMax

func TimeMax(times ...time.Time) time.Time

func TimeMin

func TimeMin(times ...time.Time) time.Time

func ToJSON

func ToJSON(data any) ([]byte, error)

func ToJSONString

func ToJSONString(data any) (string, error)

func ToJSONStringNoError

func ToJSONStringNoError(data any) string

func Today

func Today() string

func TouchFile

func TouchFile(fileName string) error

func UUID

func UUID() string

func Uniq

func Uniq[T comparable](slice []T) []T

func Values

func Values[K comparable, V any](m map[K]V) []V

func VerifyECDSA

func VerifyECDSA(r io.Reader, pubKey []byte, sign string) (bool, error)

func WorkerDo

func WorkerDo[K any](ctx context.Context, workerNum int, inputs []K,
	fn func(context.Context, K) error) error

func WorkerResult

func WorkerResult[K, V any](ctx context.Context, workerNum int, inputs []K,
	fn func(context.Context, K) (V, error)) ([]V, error)

func WriteKeyValueToString

func WriteKeyValueToString(data KeyVals) string

func Yesterday

func Yesterday() string

Types

type CacheProvidor

type CacheProvidor interface {
	Get(key string) ([]byte, error)
	Set(key string, data []byte, expire time.Duration) error
}

type FetchFn

type FetchFn func(context.Context) ([]byte, error)

type Float

type Float interface {
	~float32 | ~float64
}

type GithubReleaseAsset

type GithubReleaseAsset struct {
	Name        string    `json:"name"`
	DownloadURL string    `json:"browser_download_url"`
	ContentType string    `json:"content_type"`
	Size        int64     `json:"size"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

type GithubReleaseItem

type GithubReleaseItem struct {
	Name        string               `json:"name"`
	TagName     string               `json:"tag_name"`
	CreatedAt   time.Time            `json:"created_at"`
	PublishedAt time.Time            `json:"published_at"`
	Assets      []GithubReleaseAsset `json:"assets"`
}

func ListGithubRelease

func ListGithubRelease(owner, repo string) ([]GithubReleaseItem, error)

type Int

type Int interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type Integer

type Integer interface {
	Int | Uint
}

type KeyVals

type KeyVals map[string]string

func ReadKeyValueFromString

func ReadKeyValueFromString(data string) (KeyVals, error)

func (KeyVals) Get

func (m KeyVals) Get(key, defaultValue string) string

func (KeyVals) GetBool

func (m KeyVals) GetBool(key string) bool

func (KeyVals) GetString

func (m KeyVals) GetString(key string) string

type Number

type Number interface {
	Integer | Float
}

type Uint

type Uint interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

Jump to

Keyboard shortcuts

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