utils

package
v1.1.24 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MIT Imports: 24 Imported by: 5

README

utils 通用工具库

cbk 熔断器

cbk_test.go
TestCbkFailed

InitCbk()

var ok bool
var lastBreaked bool
for j := 0; j < 200; j++ {
	i := j
	//safego.Go(func() {
	err := Impls[SIMPLE].Check("test") //30s 返回一次true尝试
	fmt.Println(i, "Check:", ok)

	if err == nil {
		time.Sleep(time.Millisecond * 10)
		Impls[SIMPLE].Failed("test")

		if i > 105 && lastBreaked {
			Impls[SIMPLE].Succeed("test")
			lastBreaked = false
			fmt.Println(i, "Succeed")
		}
	} else {
		if lastBreaked {
			time.Sleep(time.Second * 10)
		} else {
			lastBreaked = true
		}
	}
	//})
}

checksum

crc32_test.go
TestGenerateCheckSumFile

src := "D:\\gitea_ws\\repair_dir\\dev_test_01\\1.0.0.1\\product"
checksumName := "nwjs"
checkSumPath, err := GenerateChecksumFile(context.Background(), src, checksumName)
if err != nil {
	t.Error(err)
	return
}
t.Log(checkSumPath)
TestGenerateChecksumMd5File

src := "D:\\gitea_ws\\repair_dir\\dev_test_01\\1.0.0.1\\product\\nwjs.checksum"
checksumMd5Path, err := GenerateChecksumMd5File(context.Background(), src)
if err != nil {
	t.Error(err)
	return
}
t.Log(checksumMd5Path)
TestGenerateChecksumFileWithIgnore

dirMap := make(map[string][]string)
dirMap["fullClient"] = []string{"E:\\game\\dev_test_01", "E:\\game\\dev_test_01\\fullClient"}
for code, dirs := range dirMap {
	t.Log("game: ", code)
	for _, dir := range dirs {
		t.Log("dir: ", dir)
		start := time.Now() // 获取当前时间
		checksumName := fmt.Sprintf("%v-62e204c376d4be7b1458d077", code)
		checksumMd5Path, err := GenerateChecksumFileWithIgnore(context.Background(), dir, checksumName, []string{fmt.Sprintf("%v-62e204c376d4be7b1458d077.checksum", code)})
		if err != nil {
			t.Error(err)
			return
		}
		t.Log(checksumMd5Path)
		elapsed := time.Since(start)
		t.Log("time:", elapsed)
	}
}
TestIsChecksumFileValid

src := "D:\\gitea_ws\\repair_dir\\dev_test_01\\1.0.0.1\\product\\nwjs.checksum"
md5Path := "D:\\gitea_ws\\repair_dir\\dev_test_01\\1.0.0.1\\product\\nwjs.checksum.md5"
valid := IsChecksumFileValid(context.Background(), src, md5Path)
if !valid {
	t.Error(valid)
	return
}
t.Log(valid)
TestCompareChecksumFiles

src := "D:\\gitea_ws\\repair_dir\\dev_test_01\\1.0.0.1\\product\\nwjs.checksum"
root := "D:\\gitea_ws\\repair_dir\\dev_test_01\\1.0.0.1\\product"
err := CompareChecksumFiles(context.Background(), root, src)
if err != nil {
	t.Error(err)
	return
}
testdir

csv CSV文件解析为MDB内存表

csv_parse_test.go
TestReadCsvToDataTable

dt, err := ReadCsvToDataTable(context.Background(), `goutils.log`, '\t',
	[]string{"xx", "xx", "xx", "xx"}, "xxx", []string{"xxx"})
if err != nil {
	t.Log(err)
	return
}
for _, r := range dt.Rows() {
	t.Log(r.Data())
}

rs := dt.RowsBy("xxx", "869")
for _, r := range rs {
	t.Log(r.Data())
}

t.Log(dt.Row("17"))

distlock 分布式锁

consullock_test.go
TestAquireConsulLock

l, _ := NewConsulLock("accountId", 10)
//l.Lock(15)
//l.Unlock()
ctx := context.Background()
fmt.Println("try lock 1")

fmt.Println(l.Lock(ctx, 5))
//time.Sleep(time.Second * 6)

//fmt.Println("try lock 2")
//fmt.Println(l.Lock(3))

l2, _ := NewConsulLock("accountId", 10)
fmt.Println("try lock 3")
fmt.Println(l2.Lock(ctx, 15))

l3, _ := NewConsulLock("accountId", 10)
fmt.Println("try lock 4")
fmt.Println(l3.Lock(ctx, 15))

time.Sleep(time.Minute)
filelock_test.go
TestFileLock

test_file_path, _ := os.Getwd()
locked_file := test_file_path

wg := sync.WaitGroup{}

for i := 0; i < 10; i++ {
	wg.Add(1)
	go func(num int) {
		flock := NewFileLock(locked_file, false)
		err := flock.Lock()
		if err != nil {
			wg.Done()
			fmt.Println(err.Error())
			return
		}
		fmt.Printf("output : %d\n", num)
		wg.Done()
	}(i)
}
wg.Wait()
time.Sleep(2 * time.Second)

rdslock_test.go
TestRdsLock

redis.InitRedises()
l, _ := NewRdsLuaLock("rdscdb", "accoutId", 4)
l2, _ := NewRdsLuaLock("rdscdb", "accoutId", 4)
//l.Lock(15)
//l.Unlock()
ctx := context.Background()
fmt.Println(l.Lock(ctx, 5))
fmt.Println("1getlock")
fmt.Println(l2.Lock(ctx, 5))
fmt.Println("2getlock")
time.Sleep(time.Second * 15)

//l2, _ := NewRdsLuaLock("accoutId", 15)

//t.Log(l2.Lock(5))

docgen 文档自动生成

cmd
docgen_test.go
TestGenDocTestUser

sb := strings.Builder{}
sb.WriteString(genDocTestUserQuery())
sb.WriteString(genDocTestUserCreate())
sb.WriteString(genDocTestUserUpdate())
sb.WriteString(genDocTestUserDelete())

GenDoc(context.Background(), "用户管理", "doc/testuser.md", 2, sb.String())

fsm 有限状态机

hc httpclient工具

ismtp 邮件工具

ismtp_test.go
TestSendEmail

emailauth := LoginAuth(
	"from",
	"xxxxxx",
	"mailhost.com",
)

ctype := fmt.Sprintf("Content-Type: %s; charset=%s", "text/plain", "utf-8")

msg := fmt.Sprintf("To: %s\r\nCc: %s\r\nFrom: %s\r\nSubject: %s\r\n%s\r\n\r\n%s",
	strings.Join([]string{"target@mailhost.com"}, ";"),
	"",
	"from@mailhost.com",
	"测试",
	ctype,
	"测试")

err := SendMail("mailhost.com:port", //convert port number from int to string
	emailauth,
	"from@mailhost.com",
	[]string{"target@mailhost.com"},
	[]byte(msg),
)

if err != nil {
	t.Log(err)
	return
}

return

safego 安全的go协程

snowflake

snowflake_test.go 雪花ID生成器
TestSnowflake

n, _ := NewNode(1)
t.Log(n.Generate(), ",", n.Generate(), ",", n.Generate())

tags_test.go 结构体TAG生成器

TestAutoGenTags

fmt.Println(AutoGenTags(testUser{}, map[string]TAG_STYLE{
	"json": TAG_STYLE_SNAKE,
	"bson": TAG_STYLE_UNDERLINE,
	"form": TAG_STYLE_ORIG,
}))

Documentation

Index

Constants

View Source
const (
	STRUCT_DATE_TIME_FORMAT_LAYOUT = "2006-01-02 15:04:05"
	STRUCT_DATE_FORMAT_LAYOUT      = "2006-01-02"
)

Variables

View Source
var (
	TAG_STYLE_NONE      = 0
	TAG_STYLE_ORIG      = 1
	TAG_STYLE_SNAKE     = 2
	TAG_STYLE_UNDERLINE = 3
)
View Source
var (
	StructTimeType = reflect.TypeOf(time.Now())
)

Functions

func Abs

func Abs(x int) int

func Abs64

func Abs64(x int64) int64

func AnyIndirect

func AnyIndirect(v reflect.Value) reflect.Value

func AsyncInvokeWithTimeout

func AsyncInvokeWithTimeout(timeout time.Duration, args ...func()) bool

usage: var respInfos []string

result := AsyncInvokeWithTimeout(time.Second*4, func() {
	time.Sleep(time.Second*2)
	respInfos = []string{"we add1","we add2"}
	fmt.Println("1done")
},func() {
	time.Sleep(time.Second*1)
	//respInfos = append(respInfos,"we add3")
	fmt.Println("2done")
})

fmt.Println("1alldone:",result)

func AsyncInvokesWithTimeout

func AsyncInvokesWithTimeout(timeout time.Duration, fs []func()) bool

func AutoGenTags

func AutoGenTags(vo interface{}, tagDefs map[string]TAG_STYLE) string

func BaseConvert

func BaseConvert(src interface{}, dstType reflect.Type) interface{}

func CheckKeyValueExpected

func CheckKeyValueExpected(keyValues map[string]string, keyName, defaultKeyValue string, expectKeyValues []string) bool

检查keyname的keyvalue是否符合预期值expectKeyValues,如果不存在keyvalue,使用defaultKeyValue判断

func ComposeKey

func ComposeKey(keys ...string) string

func ContextWithTrace

func ContextWithTrace() context.Context

func ContextWithTsTrace added in v1.1.14

func ContextWithTsTrace() context.Context

func ConvertFieldStyle

func ConvertFieldStyle(str string, style TAG_STYLE) string

func CopyStruct

func CopyStruct(src, dest interface{}, f StructConvFunc) error

dest 必须是指针

func CopyStructDefault

func CopyStructDefault(src, dest interface{}) error

func CopyStructs

func CopyStructs(src, dest interface{}, f StructConvFunc) error

dest 必须是数组的指针

func CopyStructsDefault

func CopyStructsDefault(src, dest interface{}) error

func CreateModel

func CreateModel(name string) interface{}

func CreateModels

func CreateModels(name string) interface{}

返回的是 结构体的数组指针 即 []*struct

func FileExist

func FileExist(filePath string) bool

func FileExt

func FileExt(filePath string) string

func GBK2UTF8

func GBK2UTF8(s []byte) ([]byte, error)

func GetCurrPath added in v1.0.14

func GetCurrPath() string

func GetModelNames

func GetModelNames() []string

func GetRegModel

func GetRegModel(name string) interface{}

func GetRegModelType

func GetRegModelType(modelName string) reflect.Type

func GetRegModels

func GetRegModels() []interface{}

func IsGBK added in v1.0.16

func IsGBK(data []byte) bool

func IsModelHasField

func IsModelHasField(modelName string, fieldName string) bool

func IsNil

func IsNil(value interface{}) bool

func IsUtf8 added in v1.0.16

func IsUtf8(data []byte) bool

func MD5

func MD5(origStr string) string

func Max

func Max(x, y int) int

func Max64

func Max64(x, y int64) int64

func MaxU

func MaxU(x, y uint32) uint32

func MergeStructs

func MergeStructs(src, dest interface{}, f StructConvFunc, keyField string, fieldMapping ...string) error

func Min

func Min(x, y int) int

func Min64

func Min64(x, y int64) int64

func MinU

func MinU(x, y uint32) uint32

func NewUUID

func NewUUID() string

func ParseContentByTag added in v1.0.14

func ParseContentByTag(content, tagStart, tagEnd string) (string, int)

func ReadImage

func ReadImage(filePath string) (image.Image, error)

func RegisterModels

func RegisterModels(models ...interface{})

func SafeIsNil

func SafeIsNil(value *reflect.Value) bool

func StringsExcept

func StringsExcept(ss1 []string, ss2 []string) (se []string)

func StringsInArray

func StringsInArray(s []string, e string) (bool, int)

func StringsReverse

func StringsReverse(s []string) []string

func UTF82GBK

func UTF82GBK(s []byte) ([]byte, error)

func WriteImage

func WriteImage(img image.Image, filePath string) error

Types

type StructConvFunc

type StructConvFunc func(interface{}, reflect.Type) interface{}

type TAG_STYLE

type TAG_STYLE = int

Directories

Path Synopsis
cmd
Package smtp implements the Simple Mail Transfer Protocol as defined in RFC 5321.
Package smtp implements the Simple Mail Transfer Protocol as defined in RFC 5321.
Package snowflake provides a very simple Twitter snowflake generator and parser.
Package snowflake provides a very simple Twitter snowflake generator and parser.

Jump to

Keyboard shortcuts

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