util

package
v1.0.15 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: Apache-2.0 Imports: 31 Imported by: 3

Documentation

Overview

Package util @Description: 工具包 @Link: https://github.com/panjf2000/ants 是一个高性能且低损耗的 goroutine 池 @Ref:

Index

Examples

Constants

This section is empty.

Variables

View Source
var C, S = "c", "s"
View Source
var Snowflake *snowflake.Node

Functions

func AddTicker

func AddTicker(tag string)

func AnyToStr

func AnyToStr(a interface{}) (string, error)

AnyToStr

@Description: 任意数据转换成字符串,默认结构化数据使用json序列化
@param a
@return string
@return error

func ConvertStringByByteSlice

func ConvertStringByByteSlice(bytes []byte) string

ConvertStringByByteSlice @Description: 字节转字符串 @param bytes @return string

func ConvertToPB

func ConvertToPB[T protoreflect.ProtoMessage](data []byte) (t T)

func CopyMeta added in v1.0.14

func CopyMeta(src, dst map[string]string)

func ExcelExport

func ExcelExport()

ExcelExport @Description: Excel导出json文件

func GenerateCSApiService

func GenerateCSApiService()

func GenerateKey

func GenerateKey() ([]byte, error)

GenerateKey 生成一个 AES 密钥。

func GenerateSnowflakeId

func GenerateSnowflakeId() string

func GeneratorAPI

func GeneratorAPI[T any](moduleName, version string, pushServices ...string)

GeneratorAPI @Description: 生成api文件 @param ref

func GeneratorRPC

func GeneratorRPC[T any](moduleName, version string)

func GetFileList

func GetFileList(path string, ext string) []string

func GetFileMd5 added in v1.0.8

func GetFileMd5(file string) string

func GetFiledName added in v1.0.11

func GetFiledName(name string) string

func GetLocalHost

func GetLocalHost() (ip string)

GetLocalHost @Description: 获取本机ip @return ip

func GetLocalHost2

func GetLocalHost2() (ip string)

func Go

func Go(f func())

Go @Description: 无序运行投递的任务, @param f

func InitGoroutinePool

func InitGoroutinePool()

func IsNil

func IsNil[Val any](val Val)

func JsonToErrorStruct added in v1.0.7

func JsonToErrorStruct(packageName, fileName, outPath string, data []TemplateKeyValueData)

func JsonToKeyValueGoFile

func JsonToKeyValueGoFile(packageName, fileName, outPath, fieldType string, data []TemplateKeyValueData)

func Max added in v1.0.4

func Max[T number](a, b T) T

Max Max[T number]

@Description: 获取最大值
@param a
@param b
@return T

func Min added in v1.0.4

func Min[T number](a, b T) T

Min Min[T number]

@Description: 获取最小值
@param a
@param b
@return T

func RandNumber added in v1.0.3

func RandNumber[T number](min, max T) T

func ReflectType

func ReflectType[T any]() reflect.Type

func RemoveAllKey

func RemoveAllKey(s []string, r string) []string

func RemoveOneKey

func RemoveOneKey(s []string, r string) []string

func SetAutoGenerateAPICSCode

func SetAutoGenerateAPICSCode(path, namespace string)

func SetExcelPath

func SetExcelPath(path string)

SetExcelPath @Description: 设置Excel文件所在路径

func SetExcelToClientJsonPath added in v1.0.7

func SetExcelToClientJsonPath(path string)

SetExcelToClientJsonPath @Description: 设置Excel导出客户端Json地址,可以追加多个输出地址 @param path

func SetExcelToGoPath

func SetExcelToGoPath(path string)

SetExcelToGoPath @Description: 设置Excel导出Go地址 @param path

func SetExcelToJsonPath

func SetExcelToJsonPath(path string)

SetExcelToJsonPath @Description: 设置Excel导出Json地址,可以追加多个输出地址 @param path

func SetExcelToUnityNamespace

func SetExcelToUnityNamespace(namespace string)

SetExcelToUnityNamespace @Description: 设置excel导出到unity的命名空间,默认不设置的话为 HotFix.Config @param namespace

func SetExcelToUnityPath

func SetExcelToUnityPath(path string)

SetExcelToUnityPath @Description: 设置Excel导出Unity地址 @param path

func SetTgfUnityFramework added in v1.0.11

func SetTgfUnityFramework()

func SliceDeduplication added in v1.0.7

func SliceDeduplication[S comparable](s []S) []S

SliceDeduplication @Description: 去重 @param s @return []S

func StrToAny

func StrToAny[T any](a string) (T, error)

StrToAny

@Description: string转任意类型 @param a @return T @return error

Example
package main

import (
	"fmt"
	"github.com/thkhxm/tgf/util"
)

//***************************************************
//@Link  https://github.com/thkhxm/tgf
//@Link  https://gitee.com/timgame/tgf
//@QQ群 7400585
//author tim.huang<thkhxm@gmail.com>
//@Description
//2023/3/6
//***************************************************

type StrToAnyStruct struct {
	Name string
	Age  int32
}

func main() {
	//to int
	if numInt, err := util.StrToAny[int]("1024"); err == nil {
		fmt.Println("numInt:", numInt)
	}

	//to interface{}
	if structData, err2 := util.StrToAny[StrToAnyStruct]("{\"Name\":\"tim\",\"Age\":5}"); err2 == nil {
		fmt.Println("json data:", structData.Name)
	}
}
Output:

numInt: 1024
json data: tim

func StringToSliceByte added in v1.0.14

func StringToSliceByte(s string) []byte

Types

type Aes added in v1.0.14

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

func NewAes added in v1.0.14

func NewAes(key []byte) (*Aes, error)

func (*Aes) DecryptAES added in v1.0.14

func (a *Aes) DecryptAES(ciphertext []byte) ([]byte, error)

func (*Aes) EncryptAES added in v1.0.14

func (a *Aes) EncryptAES(text []byte) ([]byte, error)

type CSStruct

type CSStruct struct {
	PackageImports []string
	Apis           []struct {
		Args       string
		Reply      string
		MethodName string
	}
	ModuleName      string
	ModuleNameUpper string
	PushServices    []string
}

type IWeight

type IWeight[T any] interface {
	// Roll
	// @Description: 根据权重随机出一个数据,并且减少物品的数量
	// @return T
	//
	Roll() (res IWeightData[T])
	// AllItem
	// @Description: 这里不会进行切片的拷贝,所以在使用的时候需要注意
	// @return IWeightItem[T]
	//
	AllItem() []IWeightData[T]

	TotalRatio() int32
	BaseRatio() int32
	BaseAmount() int32
	Len() int

	OnlyRoll() (res IWeightData[T])
	UpdateItemStock(data IWeightData[T])
}

type IWeightBuilder

type IWeightBuilder[T any] interface {
	AddWeight(weightRatio, amount int32, data T) IWeightBuilder[T]
	Build() IWeight[T]
	Seed(seed uint64) IWeightBuilder[T]
}

func NewWeightBuilder

func NewWeightBuilder[T any]() IWeightBuilder[T]

type IWeightData

type IWeightData[T any] interface {
	Data() T
	Amount() int32
	Ratio() int32
	BaseRatio() int32
}

type IWeightItem

type IWeightItem[T any] interface {
	IWeightData[T]
	Hit() (IWeightItem[T], bool)
}

type TemplateKeyValueData

type TemplateKeyValueData struct {
	FieldName interface{}
	Values    interface{}
	Other     interface{}
}

Jump to

Keyboard shortcuts

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