do

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2025 License: Apache-2.0 Imports: 12 Imported by: 1

README

do

Some magic features implemented using golang's generics

Contributing

You can commit PR to this repository

How to get it?
go get -u github.com/gobkc/do
Unit test?
  1. how to test all unit test cases?
go test -v
  1. how to test a specific unit test case?
go test -v -run Poller
Quick start
package main

import (
	"github.com/gobkc/do"
	"log"
)

func main() {
    a := 1
    log.Println(do.OneOf(a==1,true,false))
}

result : true

License

© Gobkc, 2023~time.Now

Released under the Apache License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AllTrue added in v0.0.4

func AllTrue(bs ...bool) bool

AllTrue AllTrue(true,true,true) == true AllTrue AllTrue(true,false,true) == false

func AnyInList added in v0.1.5

func AnyInList[T comparable]()

func AnyTrue added in v0.0.4

func AnyTrue(bs ...bool) bool

AnyTrue AnyTrue(true,false,false) == true AnyTrue AnyTrue(false,false,false) == false

func ErrorOr added in v0.0.3

func ErrorOr(err error, val string) string

func GetFieldList added in v0.1.2

func GetFieldList[T any, R any](items []T, fieldGetter func(T) R) []R

GetFieldList is a generic function that takes a slice of items and a fieldGetter function, and returns a slice of any field type, such as string, int64, etc. examples

names1 := GetFieldList(items1, func(item Item) string {
	return item.Name
})

// Retrieve the Age field list from items1 (int64 type)

func GetFieldMap added in v0.1.2

func GetFieldMap[T any, K comparable](items []T, fieldGetter func(T) K) map[K]T

func GetFieldMaps added in v0.1.2

func GetFieldMaps[T any, K comparable](items []T, fieldGetter func(T) K) map[K][]T

func GetStructName added in v0.0.7

func GetStructName(d any) string

func InList added in v0.0.4

func InList[T comparable](item T, list ...T) bool

InList InList["a",[]string{"a","b"}] == true

func OneOf

func OneOf[T any](condition bool, result1 T, result2 T) T

func OneOr added in v0.0.2

func OneOr[T any](result1 T, result2 T) T

func RegexpCheck added in v0.1.6

func RegexpCheck(pattern string, str string) bool

RegexpCheck Use regular expressions to determine if a string matches Example: RegexpCheck(`(?i)^[a-zA-Z]+ (asc|desc)$`,`dafd Asc`) == true

func RegexpConvertSnake added in v0.1.7

func RegexpConvertSnake(s string) string

RegexpConvertSnake convert string to snake case Example: RegexpConvertSnake(`AbC`) == `ab_c`

func ReplaceMap added in v0.1.5

func ReplaceMap(s string, replace map[string]string) (result string, err error)

Types

type DiffResp added in v0.1.1

type DiffResp[T comparable] struct {
	Added   []T
	Deleted []T
	Same    []T
}

func Diff added in v0.1.1

func Diff[T comparable](olds, news []T) (resp DiffResp[T])

Diff The diff function is used to analyze the items to be added and deleted between two slices. example: var news = []string{`aa1`, `aa2`, `aa3`} var olds = []string{`aa1`, `aa4`} diffs := Diff(olds, news) result:added: aa2,aa3, deleted:aa4, same: aa1

type LeaderPoller added in v0.1.3

type LeaderPoller[T any] struct {
	// contains filtered or unexported fields
}

func NewLeaderPoller added in v0.1.3

func NewLeaderPoller[T any](fs func(setting *LeaderPollerSetting)) *LeaderPoller[T]

func (*LeaderPoller[T]) Conditions added in v0.1.3

func (lp *LeaderPoller[T]) Conditions(conditionFunctions ...func() (T, error))

func (*LeaderPoller[T]) Run added in v0.1.3

func (lp *LeaderPoller[T]) Run(task func(T))

type LeaderPollerSetting added in v0.1.3

type LeaderPollerSetting struct {
	Client   *redis.Client
	Interval time.Duration
	Subject  string
	Key      string
	Delayed  bool
}

type PollerImpl added in v0.0.8

type PollerImpl[T any] struct {
	// contains filtered or unexported fields
}

func Poller added in v0.0.8

func Poller[QueryResult any](query func(q *QueryResult) error) *PollerImpl[QueryResult]

func (*PollerImpl[T]) Catch added in v0.0.8

func (p *PollerImpl[T]) Catch(f func(err error))

func (*PollerImpl[T]) Setting added in v0.0.8

func (p *PollerImpl[T]) Setting(f func(settings *PollerSetting)) *PollerImpl[T]

func (*PollerImpl[T]) Stop added in v0.0.8

func (p *PollerImpl[T]) Stop()

func (*PollerImpl[T]) Then added in v0.0.8

func (p *PollerImpl[T]) Then(f func(result *T)) *PollerImpl[T]

type PollerSetting added in v0.0.8

type PollerSetting struct {
	Interval time.Duration
}

type ReTry added in v0.0.7

type ReTry[T any] struct {
	sync.Once
	// contains filtered or unexported fields
}

func (*ReTry[T]) Keep added in v0.0.7

func (r *ReTry[T]) Keep(action func(t *T) error) *T

Keep example

s := retry.Keep(func(t *Test) error {
	if err := GenError(); err != nil {
		return err
	}
	t.Name = `abc`
	return nil
})

func (*ReTry[T]) Times added in v0.0.7

func (r *ReTry[T]) Times(times int, action func(t *T) error) *T

Jump to

Keyboard shortcuts

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