gm

package module
v0.0.25 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: GPL-2.0, GPL-3.0 Imports: 15 Imported by: 0

README

go-modules

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BeginGormTx

func BeginGormTx() *gorm.DB

func BytesEqual added in v0.0.8

func BytesEqual(a, b []byte) bool

func CommitGormTx

func CommitGormTx(tx *gorm.DB) error

func FilterOne added in v0.0.6

func FilterOne[T any](s []T, fn func(T) bool) (t T)

Returns the first element that meet the condition specified in a callback function.

func HashArrayToArrays

func HashArrayToArrays[T any](input []T, count int) [][]T

Hash an array into a nested array

numArray := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
splitArray := gm.HashArrayToArrays(numArray, 3)
fmt.Printf("new array: %v\n", splitArray)

Output: [[1 4 7 9] [2 5 10] [3 6 8]]

func InitGorm

func InitGorm(host string, port int64, user, password, db_name string) (err error)

func Map

func Map[T any, T2 any](s []T, fn func(t T) (T2, error)) (t2 []T2, err error)

Calls a defined callback function on each element of an array, and returns an array that contains the results.

func StringArrayToNumberArray() {
	var stringArray []string = []string{"1", "2", "3"}

	numberArray, err := Map(stringArray, func(s string) (int64, error) {
		return strconv.ParseInt(s, 10, 64)
	})
	if err != nil {
		fmt.Println("string array to number array error, msg: ", err)
		return
	}
	fmt.Println(numberArray)
}

func CalculateSquareValue() {
	var numArray []int64 = []int64{2, 3, 4}
	squareArray, _ := Map(numArray, func(n int64) (int64, error) {
		return (n * n), nil
	})
	fmt.Println(squareArray)
}

func NewGormSession

func NewGormSession() *gorm.DB

func NewNumber added in v0.0.18

func NewNumber[T number](v T) T

func NewNumberPtr added in v0.0.18

func NewNumberPtr[T number](v T) *T

func NewStringPtr added in v0.0.18

func NewStringPtr(v string) *string

func NewUint64 added in v0.0.18

func NewUint64[T number](v T) uint64

func ReadFileBytes

func ReadFileBytes(filePath string) ([]byte, error)

func ReadFileHash

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

Get the hash value of the file The effect is equivalent to the command line sha256sum

fileBytes, err := gm.ReadFileBytes("example.go")
if err != nil {
	fmt.Printf("read file bytes err, msg: %v\n", err)
	return
}

hash, err := gm.ReadFileHash(fileBytes)
if err != nil {
	fmt.Printf("read file hash err, msg: %v\n", err)
	return
}
fmt.Printf("hash: %v\n", hash)

-----------------------------------------------------
FILE_HASH=$(sha256sum example.go)

hash == FILE_HASH

func TruncateString added in v0.0.6

func TruncateString(v string, start, end int) string

Truncate the string, keep the beginning and ends, and add an ellipsis in the middle to truncate the string

func UniqueAppend added in v0.0.7

func UniqueAppend[S ~[]E, E comparable](s S, v E) S

Only add the element if it is not already in the slice

func ValidateJsonUnmarshal

func ValidateJsonUnmarshal(data []byte, v interface{}) error

Validate JSON data and parse it into the specified structure

func ValidateStruct

func ValidateStruct(v interface{}) error

Verify the structure

Types

type RedisClient added in v0.0.16

type RedisClient struct {
	Context context.Context
	// contains filtered or unexported fields
}

func InitRedis added in v0.0.16

func InitRedis[T number](ctx context.Context, username, password, address string, db T) (redisClient *RedisClient, err error)

func InitRedisFromURL added in v0.0.16

func InitRedisFromURL(ctx context.Context, url string) (redisClient *RedisClient, err error)

func (*RedisClient) Client added in v0.0.17

func (redisClient *RedisClient) Client() *redis.Client

Base Commands ------------------------------------------------------------------------------

func (*RedisClient) DBSize added in v0.0.17

func (redisClient *RedisClient) DBSize() *redis.IntCmd

func (*RedisClient) Del added in v0.0.16

func (redisClient *RedisClient) Del(keys ...string) *redis.IntCmd

func (*RedisClient) Get added in v0.0.16

func (redisClient *RedisClient) Get(key string) *redis.StringCmd

func (*RedisClient) HDel added in v0.0.16

func (redisClient *RedisClient) HDel(key string, field ...string) *redis.IntCmd

func (*RedisClient) HExists added in v0.0.16

func (redisClient *RedisClient) HExists(key string, field string) *redis.BoolCmd

func (*RedisClient) HGet added in v0.0.16

func (redisClient *RedisClient) HGet(key string, field string) *redis.StringCmd

func (*RedisClient) HGetAll added in v0.0.16

func (redisClient *RedisClient) HGetAll(key string) *redis.StringStringMapCmd

func (*RedisClient) HKeys added in v0.0.16

func (redisClient *RedisClient) HKeys(key string) *redis.StringSliceCmd

------------------------------------------------------------------------------

func (*RedisClient) HLen added in v0.0.16

func (redisClient *RedisClient) HLen(key string) *redis.IntCmd

func (*RedisClient) HSet added in v0.0.16

func (redisClient *RedisClient) HSet(key string, value ...interface{}) *redis.IntCmd

func (*RedisClient) Keys added in v0.0.16

func (redisClient *RedisClient) Keys(parttern string) *redis.StringSliceCmd

func (*RedisClient) MGet added in v0.0.16

func (redisClient *RedisClient) MGet(keys ...string) *redis.SliceCmd

func (*RedisClient) MSet added in v0.0.16

func (redisClient *RedisClient) MSet(values ...interface{}) *redis.StatusCmd

------------------------------------------------------------------------------

func (*RedisClient) NewTxPipeline added in v0.0.16

func (redisClient *RedisClient) NewTxPipeline() *RedisTxPipeline

func (*RedisClient) Set added in v0.0.16

func (redisClient *RedisClient) Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd

type RedisTxPipeline added in v0.0.16

type RedisTxPipeline struct {
	Context context.Context
	// contains filtered or unexported fields
}

Redis Transaction Pipeline

func (*RedisTxPipeline) Commit added in v0.0.16

func (pipeline *RedisTxPipeline) Commit() ([]redis.Cmder, error)

func (*RedisTxPipeline) Del added in v0.0.16

func (pipeline *RedisTxPipeline) Del(keys ...string) *redis.IntCmd

func (*RedisTxPipeline) Get added in v0.0.16

func (pipeline *RedisTxPipeline) Get(key string) *redis.StringCmd

func (*RedisTxPipeline) HDel added in v0.0.16

func (pipeline *RedisTxPipeline) HDel(key string, field ...string) *redis.IntCmd

func (*RedisTxPipeline) HExists added in v0.0.16

func (pipeline *RedisTxPipeline) HExists(key string, field string) *redis.BoolCmd

func (*RedisTxPipeline) HGet added in v0.0.16

func (pipeline *RedisTxPipeline) HGet(key string, field string) *redis.StringCmd

func (*RedisTxPipeline) HGetAll added in v0.0.16

func (pipeline *RedisTxPipeline) HGetAll(key string) *redis.StringStringMapCmd

func (*RedisTxPipeline) HKeys added in v0.0.16

func (pipeline *RedisTxPipeline) HKeys(key string) *redis.StringSliceCmd

------------------------------------------------------------------------------

func (*RedisTxPipeline) HLen added in v0.0.16

func (pipeline *RedisTxPipeline) HLen(key string) *redis.IntCmd

func (*RedisTxPipeline) HSet added in v0.0.16

func (pipeline *RedisTxPipeline) HSet(key string, value ...interface{}) *redis.IntCmd

func (*RedisTxPipeline) Keys added in v0.0.16

func (pipeline *RedisTxPipeline) Keys(parttern string) *redis.StringSliceCmd

func (*RedisTxPipeline) MGet added in v0.0.16

func (pipeline *RedisTxPipeline) MGet(keys ...string) *redis.SliceCmd

func (*RedisTxPipeline) MSet added in v0.0.16

func (pipeline *RedisTxPipeline) MSet(values ...interface{}) *redis.StatusCmd

------------------------------------------------------------------------------

func (*RedisTxPipeline) Pipeliner added in v0.0.17

func (pipeline *RedisTxPipeline) Pipeliner() redis.Pipeliner

func (*RedisTxPipeline) Set added in v0.0.16

func (pipeline *RedisTxPipeline) Set(key string, value interface{}, expiration time.Duration) *redis.StatusCmd

Jump to

Keyboard shortcuts

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