Documentation
¶
Overview ¶
nolint: wrapcheck
Package utils provides Minimalist and zero-dependency utility functions.
Index ¶
- func Add0xPrefix(input string) string
- func ByteSize(bytes uint64) string
- func CopyBytes(b []byte) []byte
- func CopyMapOfArray[K comparable, V any](src map[K][]V) map[K][]V
- func CopyString(s string) string
- func DecodeHex(str string) ([]byte, error)
- func Default[T comparable](value T, defaultValue T) (result T)
- func DefaultOptional[T comparable](opt []T, defaultValue T) (result T)
- func DerefPtr[T any](ptr *T) T
- func DerefPtrOr[T any](ptr *T, def T) T
- func Empty[T any]() T
- func EmptyableToPtr[T any](x T) *T
- func EqualPtr[T comparable](a, b *T) bool
- func Error[T any](data T, err error) error
- func Flip0xPrefix(input string) string
- func FromPtr[T any](x *T) T
- func FromPtrOr[T any](x *T, fallback T) T
- func Has0xPrefix(input string) bool
- func IsHex(str string) bool
- func IsNil(i interface{}) bool
- func IsStruct(s interface{}) bool
- func Match(pattern, value string) bool
- func MatchTwoAsterisk(pattern, value string) bool
- func Merge[T comparable](to, from T) T
- func Must[T any](data T, err any, messageArgs ...interface{}) T
- func MustNotError[T any](data T, err error) T
- func MustNotOK[T any](data T, ok bool) T
- func MustOK[T any](data T, ok bool) T
- func OK[T any](data T, ok bool) bool
- func Optional[T any](opt []T) (optional T, ok bool)
- func PrettyStruct(s interface{}) string
- func PtrOf[T any](v T) *T
- func RandomBytes(length int) []byte
- func RandomHex(length int) string
- func RandomString(n int) string
- func Sleep(ctx context.Context, d time.Duration) error
- func StructHasZero(s any, includeNested ...bool) bool
- func StructZeroFields(s any, checkNested ...bool) []string
- func ToPtr[T any](v T) *T
- func ToString(arg interface{}, args ...any) string
- func ToZero[T any](value T) (result T)
- func Trim0xPrefix(input string) string
- func UnsafeBytes(s string) (bs []byte)
- func UnsafeMust[T any, E any](data T, e E) T
- func UnsafeString(b []byte) string
- func Zero[T any](value T) T
- type FuncMap
- type Template
- func (t *Template) Execute(wr io.Writer, args interface{}) error
- func (t *Template) ExecuteHTML(wr io.Writer, args interface{}) error
- func (t *Template) HTML(args interface{}) (string, error)
- func (t *Template) ParsedTemplate() (*texttemplate.Template, error)
- func (t *Template) ParsedTemplateHTML() (*htmltemplate.Template, error)
- func (t *Template) String() string
- func (t *Template) Text(args interface{}) (string, error)
- type TemplateConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add0xPrefix ¶
Add0xPrefix returns the input string with 0x prefix.
func CopyMapOfArray ¶
func CopyMapOfArray[K comparable, V any](src map[K][]V) map[K][]V
CopyMapOfArray high performance copy map of array to new map of array with shared backing array. Reference: https://cs.opensource.google/go/go/+/master:src/net/http/header.go;l=94
func Default ¶
func Default[T comparable](value T, defaultValue T) (result T)
Default inspired by Nullish coalescing operator (??) in JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing
func DefaultOptional ¶
func DefaultOptional[T comparable](opt []T, defaultValue T) (result T)
DefaultOptional extract optional parameter from variadic function parameter. If parameter is not provided or zero value of type T, return defaultValue.
It's useful to reduce boilerplate code when implementing optional parameter. You won't need to check if parameter is provided or not.
func DerefPtr ¶
func DerefPtr[T any](ptr *T) T
DerefPtr dereferences ptr and returns the value it points to if no nil, or else returns def.
func DerefPtrOr ¶
func DerefPtrOr[T any](ptr *T, def T) T
DerefPtrOr dereferences ptr and returns the value it points to if no nil, or else returns def.
func EmptyableToPtr ¶
func EmptyableToPtr[T any](x T) *T
EmptyableToPtr returns a pointer copy of value if it's nonzero. Otherwise, returns nil pointer.
func EqualPtr ¶
func EqualPtr[T comparable](a, b *T) bool
Equal returns true if both arguments are nil or both arguments dereference to the same value.
func Flip0xPrefix ¶
Flip0xPrefix returns the input string with 0x prefix if it doesn't have 0x prefix, otherwise returns the input string without 0x prefix.
func FromPtr ¶
func FromPtr[T any](x *T) T
FromPtr alias of DerefPtr. returns the pointer value or empty.
func FromPtrOr ¶
func FromPtrOr[T any](x *T, fallback T) T
FromPtrOr alias of DerefPtrOr. returns the pointer value or the fallback value.
func Has0xPrefix ¶
Has0xPrefix checks if the input string has 0x prefix or not.
Returns `true“ if the input string has 0x prefix, otherwise `false`.
func IsNil ¶
func IsNil(i interface{}) bool
IsNil checks if the given interface value is literally nil.
func IsStruct ¶
func IsStruct(s interface{}) bool
IsStruct returns true if the given variable is a struct or *struct.
func Match ¶
Match matches patterns as gitignore pattern. Reference https://git-scm.com/docs/gitignore, https://gist.github.com/jstnlvns/ebaa046fae16543cc9efc7f24bcd0e31
func MatchTwoAsterisk ¶
func Merge ¶
func Merge[T comparable](to, from T) T
Merge merges two structs with the same type. merge from -> to.
warning: this function will modify the first struct (to)
func Must ¶
Must is used to simplify error handling. It's helpful to wraps a call to a function returning a value and an error. and panics if err is error or false.
warning: this is not safe, use with caution!! (avoid to use it's in runtime)
func MustNotError ¶
MustNotError is used to simplify error handling.
warning: this is not safe, use with caution!! (avoid to use it's in runtime)
func MustNotOK ¶
MustNotOK is used to simplify ok handling. for case ok should be false.
warning: this is not safe, use with caution!! (avoid to use it's in runtime)
func MustOK ¶
MustOK is used to simplify ok handling. for case ok should be true.
warning: this is not safe, use with caution!! (avoid to use it's in runtime)
func Optional ¶
Optional extract optional parameter from variadic function parameter. If parameter is not provided, return zero value of type T and false. If parameter is provided, return parameter and true.
Example: func Get(key string, option ...Option) (string, error) { ... }
It's useful to reduce boilerplate code when implementing optional parameter. You won't need to check if parameter is provided or not.
func PrettyStruct ¶
func PrettyStruct(s interface{}) string
PrettyStruct returns pretty json string of the given struct.
func RandomBytes ¶
RandomBytes returns a random byte slice with the given length with crypto/rand.
func RandomString ¶
func Sleep ¶
Sleep is a time.Sleep, but it can be interrupted by context. If interrupted, Sleep returns ctx.Err().
ctx, cancel := context.WithCancel(context.Background()) defer cancel() // do something every second until do something else finish. go func() { for { doSomething() if err := Sleep(ctx, 1 * time.Second); err != nil { return // interrupted } } }() doSomethingElse() cancel() // interrupt the goroutine
func StructHasZero ¶
StructHasZero returns true if a field in a struct is zero value(has not initialized) if includeNested is true, it will check nested struct (default is false)
func StructZeroFields ¶
StructZeroFields returns name of fields if that's field in a struct is zero value(has not initialized) if checkNested is true, it will check nested struct (default is false)
func ToZero ¶
func ToZero[T any](value T) (result T)
ToZero alias of Zero. returns zero value of the type of the given value.
func Trim0xPrefix ¶
Trim0xPrefix returns the input string without 0x prefix.
func UnsafeBytes ¶
UnsafeBytes returns a byte pointer without allocation
func UnsafeMust ¶
UnsafeMust is used to simplify error/ok handling by ignoring it in runtime.
warning: this is not safe, use with caution!! be careful when value is pointer, it may be nil. (safe in runtime, but need to check nil before use)
func UnsafeString ¶
UnsafeString returns a string pointer without allocation
Types ¶
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template is a wrapper of text/template and html/template.
func NewTemplate ¶
func NewTemplate(name string, tmpl string, config TemplateConfig) *Template
NewTemplate is a wrapper of text/template and html/template. used to define a template at initialization time without error checking.
Example:
package main import "github.com/Cleverse/go-utilities/utils" var HelloWorldTemplate = utils.NewTemplate("hello", "Hello {{.Name}}", utils.TemplateConfig{}) func main() { ... }
func (*Template) Execute ¶
Execute applies a parsed template to the specified data object and writes the output to wr.
func (*Template) ExecuteHTML ¶
ExecuteHTML applies a parsed template to the specified data object and writes the output to wr.
func (*Template) HTML ¶
HTML applies a parsed template to the specified data object and returns the output as a string.
func (*Template) ParsedTemplate ¶
func (t *Template) ParsedTemplate() (*texttemplate.Template, error)
func (*Template) ParsedTemplateHTML ¶
func (t *Template) ParsedTemplateHTML() (*htmltemplate.Template, error)