util

package
v0.0.0-...-8c930d8 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

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 builds the TLS configuration a client dials with: the certificate and key pair when both files are given, the CA bundle when caFile is, and insecureSkipVerify as given.

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 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 FormatDurationSmart

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

FormatDurationSmart formats a time.Duration into a string, picking the unit that keeps the value readable:

  • Less than 1µs: nanoseconds (e.g., "900ns").
  • Less than 1ms: microseconds with the given precision (e.g., "18.40µs").
  • Less than 1s: milliseconds with the given precision (e.g., "123.451ms").
  • Less than 1min: seconds with the given precision (e.g., "2.013s").
  • 1min or more: minutes with the given precision (e.g., "1.502min").

Nanoseconds are the resolution of time.Duration itself, so that tier prints an integer and ignores precision; decimals there would only claim a resolution that does not exist.

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

func PanicError

func PanicError(recovered any) error

PanicError turns a recovered panic value into an error carrying the stack of the panic site. It must be called from the deferred function that recovered, while the goroutine is still unwinding: the frames captured then still include the line that panicked, whereas a stack taken after recovery would only show the recovering package. A panic value that is already an error keeps its own, deeper stack if it has one — the error_stack field reports the deepest stack in the chain — and gains this one otherwise.

func SpanID

func SpanID() string

SpanID returns a new W3C span id, 8 random bytes in lower-case hex, never all zero, such as 00f067aa0ba902b7.

func TraceID

func TraceID() string

TraceID returns a new W3C trace id for an execution outside any trace, a request that brought none, a cron round or a leader tenure: 16 random bytes in lower-case hex, never all zero, such as 4bf92f3577b34da6a3ce929d0e0e4736, the form the X-Trace-ID header carries and the propagator accepts.

Types

This section is empty.

Jump to

Keyboard shortcuts

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