goutil

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: GPL-3.0 Imports: 10 Imported by: 0

README

goutil: Your Go-To Utility Library for Go

GitHub go.mod Go version CI codecov Go Report Card Go Reference

Introduction

doc online

goutil is a comprehensive, easy-to-use utility library for Go programmers. It provides a collection of handy utility functions that are commonly used in day-to-day programming tasks. This library aims to enhance productivity by reducing the time spent on writing and maintaining common utility functions.

Features

  • Comprehensive: Covers a wide range of functionalities including string manipulation, file I/O operations, data structure operations, and error handling.
  • Easy to Use: Designed with simplicity and ease of use in mind.
  • Time-Saving: Reduces the time spent on writing and maintaining common utility functions.

Installation

To install goutil, use the following command:

go get github.com/qcrao/goutil

Usage

Import goutil in your Go program as follows:

import "github.com/qcrao/goutil"

You can then call the utility functions as per your requirement. For example:

result := goutil.SomeUtilityFunction(input)

go test

go test -race -coverprofile=coverage.txt -covermode=atomic ./... && go tool cover -html=coverage.txt

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultRetry   = BackoffWait{TotalRuns: 2, BaseDuration: time.Second, Factor: 2.0, JitterFactor: 0.1}
	FastRetry      = BackoffWait{TotalRuns: 2, BaseDuration: 50 * time.Millisecond, Factor: 2.0, JitterFactor: 0.5}
	UnlimitedRetry = BackoffWait{TotalRuns: math.MaxInt32, BaseDuration: time.Second, Factor: 2.0, JitterFactor: 0.5}
	NoRetry        = BackoffWait{TotalRuns: 1}
)
View Source
var Dump = spew.Dump
View Source
var ErrInvalidBatchType = errors.New("invalid batch type")
View Source
var ErrNotSetDeadline = errors.New("context doesn't set deadline")
View Source
var ErrTimeout = errors.New("timed out waiting for the condition")
View Source
var Fdump = spew.Fdump
View Source
var Sdump = spew.Sdump

Functions

func Bytes2Str

func Bytes2Str(b []byte) string

Bytes2Str converts a byte slice to a string without making a copy of the original slice data. This is achieved by reinterpreting the slice header to a string header. Caution: this function uses the unsafe package and may cause unexpected errors.

func Float64Equal

func Float64Equal(f1 float64, f2 float64, optionalPrecision ...uint8) bool

Float64Equal compares two float64 numbers for equality to a certain number of decimal places. It takes two float64 numbers f1 and f2, and an optional uint8 precision. If precision is not provided, it defaults to defaultPrecision. The function returns true if the absolute difference between f1 and f2 is less than or equal to the threshold (10 to the power of -precision), indicating that they are equal to the specified number of decimal places. It returns false otherwise.

func Go

func Go(fn func())

Go starts a goroutine with recovery capability. If the goroutine panics, it will recover and use a default error handler.

func GoWithErrorHandler

func GoWithErrorHandler(fn func(), errorHandler func(err interface{}))

GoWithErrorHandler starts a goroutine with a custom error handler. If the goroutine panics, it will recover and use the provided error handler.

func RetryWithExponentialBackoff added in v0.0.2

func RetryWithExponentialBackoff(backoff BackoffWait, fn RetryableFunc) (err error)

RetryWithExponentialBackoff tries a function with exponential backoff, and return the error from the function or timeout.

func RetryWithExponentialBackoffUntilTimeout added in v0.0.2

func RetryWithExponentialBackoffUntilTimeout(ctx context.Context, f RetryableFuncWithContext) (err error)

RetryWithExponentialBackoffUntilTimeout tries a function with exponential backoff until it succeeds or the context is cancelled (timeout).

func Str2Bytes

func Str2Bytes(s string) []byte

Str2Bytes converts a string to a byte slice without making a copy of the original string data. This is achieved by reinterpreting the string header to a slice header. Caution: this function uses the unsafe package and may cause unexpected errors.

Types

type BackoffWait added in v0.0.2

type BackoffWait struct {
	TotalRuns    int           `json:"total_runs"`
	BaseDuration time.Duration `json:"base_duration"`
	Factor       float64       `json:"factor"`
	JitterFactor float64       `json:"jitter_factor"`
}

BackoffWait encapsulates parameters that control the behavior of backoff mechanism. TotalRuns denotes the maximum number of times the function is executed, BaseDuration is the initial waiting time before function execution, Factor is the multiplier for exponential growth of waiting time, JitterFactor is the factor for random increase to the waiting time.

type BatchProcessor added in v0.0.3

type BatchProcessor interface {
	SplitBatch(items interface{}, batchSize int) []interface{}
	Process(ctx context.Context, batch interface{}) (BatchResult, error) // batch 本身是一个 slice
}

BatchProcessor defines the interface for batch processing.

type BatchResult added in v0.0.3

type BatchResult interface {
	Merge(BatchResult)
}

BatchResult defines the interface for merging batch results.

func BatchProcess added in v0.0.3

func BatchProcess(ctx context.Context, items interface{}, batchSize int, concurrency int, processor BatchProcessor) (BatchResult, error)

BatchProcess performs the batch processing.

type BatchSplitter added in v0.0.3

type BatchSplitter interface {
	SplitBatch(items interface{}, batchSize int) []interface{}
}

BatchSplitter defines the interface for splitting items into batches.

type Int64Split added in v0.0.3

type Int64Split struct{}

Int64Split is used to split batches of type []int64.

func (*Int64Split) SplitBatch added in v0.0.3

func (s *Int64Split) SplitBatch(items interface{}, batchSize int) []interface{}

SplitBatch splits the []int64 into batches.

type RetryableFunc added in v0.0.2

type RetryableFunc func() (done bool, err error)

RetryableFunc is a function type that can be retried until it succeeds or meets a certain condition.

func (RetryableFunc) WithContext added in v0.0.2

func (cf RetryableFunc) WithContext() RetryableFuncWithContext

type RetryableFuncWithContext added in v0.0.2

type RetryableFuncWithContext func(context.Context) (done bool, err error)

RetryableFuncWithContext is a RetryableFunc that includes context, which can be used for cancellation or passing request-scoped data.

Jump to

Keyboard shortcuts

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