util

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: Apache-2.0 Imports: 32 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildTLSConfig

func BuildTLSConfig(certFile, keyFile, caFile string, insecureSkipVerify bool) (*tls.Config, error)

BuildTLSConfig creates a TLS configuration from the etcd config

func BytesToString

func BytesToString(b []byte) string

BytesToString converts byte slice to string without a memory allocation. For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.

func CharSpliter

func CharSpliter(data []byte, atEOF bool) (advance int, token []byte, err error)

CharSpliter is the custom split function for bufio.Scanner.

func CheckErr

func CheckErr(err error)

CheckErr just check error and print it.

func CombineError

func CombineError(fns ...func() error) error

CombineError combine error from fns.

func Contains

func Contains[T comparable](slice []T, elem T) bool

Contains check T in slice.

func Depointer

func Depointer[T comparable](t *T) T

Depointer will return a T with given value.

func Deref

func Deref[T any](p *T) T

Deref returns the value pointed to by the pointer. If the pointer is nil, returns the zero value for the type.

func DerefOr

func DerefOr[T any](p *T, defaultVal T) T

DerefOr returns the value pointed to by the pointer. If the pointer is nil, returns the default value.

func FileExists

func FileExists(filename string) bool

FileExists check file exists.

func FormatDurationSmart

func FormatDurationSmart(d time.Duration, precisions ...int) string

FormatDurationSmart formats a time.Duration into a string with the following rules:

  • If duration is less than 1ms, display as milliseconds with the specified precision (e.g., "0.018ms").
  • If duration is less than 1s, display as milliseconds with the specified precision (e.g., "123.451ms").
  • If duration is less than 1min, display as seconds with the specified precision (e.g., "2.013s").
  • If duration is 1min or more, display as minutes with the specified precision (e.g., "1.502min").

Negative durations are supported and formatted with a '-' sign. The precision parameter controls the number of digits after the decimal point (minimum 0, maximum 9).

func GetFdFromConn

func GetFdFromConn(c net.Conn) int

GetFdFromConn get net.Conn's file descriptor.

func GetFdFromListener

func GetFdFromListener(l net.Listener) int

GetFdFromListener get net.Listener's file descriptor.

func GetFunctionName

func GetFunctionName(x any) string

func HandleErr

func HandleErr(err error, notExit ...bool)

HandleErr will call os.Exit() when any error encountered.

func HashID

func HashID(fields ...string) string

func IPv6ToIPv4

func IPv6ToIPv4(ipStr string) string

IPv6ToIPv4 converts IPv6 to IPv4 if possible

func IndexedUUID

func IndexedUUID() string

func IsConnClosed

func IsConnClosed(err error) bool

func Marshal

func Marshal(value any) ([]byte, error)

func NoError

func NoError(fn func() error) error

NoError call fn and always return nil.

func ParseScheme

func ParseScheme(req *http.Request) string

func Ping

func Ping(ip string, timeout time.Duration) (bool, error)

Ping work like command `ping`. If target ip is reachable, return true, nil, If target ip is unreachable, return false, nil, If error encountered, return false, error. More usage see tests in `pkg/util/util_test.go`.

func Pointer

func Pointer[T comparable](t T) *T

Pointer will return a pointer to T with given value.

func Recovery

func Recovery(name string)

Recovery global Recovery recover panic

func RequestID

func RequestID() string

func Round

func Round[T float32 | float64](value T, precision uint) T

Round returns a rounded version of x with a specified precision.

The precision parameter specifies the number of decimal places to round to. Round uses the "round half away from zero" rule to break ties.

Examples:

Round(3.14159, 2) returns 3.14
Round(3.14159, 1) returns 3.1
Round(-3.14159, 1) returns -3.1
Example
fmt.Printf("%.3f\n", Round(3.14159, 3))
fmt.Printf("%.2f\n", Round(2.71828, 2))
fmt.Printf("%.1f\n", Round(-3.14159, 1))
Output:

3.142
2.72
-3.1

func RunOrDie

func RunOrDie(fn func() error)

RunOrDie will panic when error encountered.

func SafeGo

func SafeGo(fn func(), names ...any)

func SafePointer

func SafePointer[T any](v T) T

func SpanID

func SpanID() string

func SplitByDoublePipe

func SplitByDoublePipe(data []byte, atEOF bool) (advance int, token []byte, err error)

SplitByDoublePipe is the custom split function for bufio.Scanner.

func StringAny

func StringAny(x any) string

StringAny format anything to string.

func StringToBytes

func StringToBytes(s string) []byte

StringToBytes convers string to byte slice without a memory allocation. For more details, see https://github.com/golang/go/issues/53003#issuecomment-1140276077.

func Tcping

func Tcping(host string, port int, timeout time.Duration) bool

_tcping work like command `tcping`.

func TraceFunction

func TraceFunction(ctx context.Context, functionName string, fn func(context.Context) error) error

TraceFunction is a helper to trace function execution

func TraceFunctionWithResult

func TraceFunctionWithResult[T any](ctx context.Context, functionName string, fn func(context.Context) (T, error)) (T, error)

TraceFunctionWithResult is a helper to trace function execution with result

func TraceID

func TraceID() string

func UUID

func UUID(prefix ...string) string

func Unmarshal

func Unmarshal(data []byte, value any) error

func ValueOf

func ValueOf[T any](v T) *T

ValueOf returns a pointer to the given value

Types

type Connection

type Connection struct {
	RemoteIP   string
	LocalIP    string
	RemotePort int
	LocalPort  int
}

func GetConnection

func GetConnection(conn net.Conn) Connection

Jump to

Keyboard shortcuts

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