utils

package
v1.22.2 Latest Latest
Warning

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

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

Documentation

Overview

Package utils is an assortment of independent utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyToString added in v1.13.1

func AnyToString(o any) string

AnyToString returns the string representation of the object. It looks for a TextMarshaler or Stringer interfaces first before defaulting to fmt.Sprintf.

func IsLowerCaseIdentifier

func IsLowerCaseIdentifier(id string) bool

IsLowerCaseIdentifier accepts only lowerCaseIdentifiers.

func IsNil added in v1.17.0

func IsNil(x any) bool

IsNil returns true if x is nil or an interface holding nil.

func IsUpperCaseIdentifier

func IsUpperCaseIdentifier(id string) bool

IsUpperCaseIdentifier accepts only UpperCaseIdentifiers.

func LooksLikeJWT added in v1.13.1

func LooksLikeJWT(token string) bool

LooksLikeJWT checks if the token is likely to be a signed representation of a JWT.

func RandomIdentifier added in v1.22.0

func RandomIdentifier(length int) string

RandomIdentifier generates a random string of the specified length. The string will include only alphanumeric characters a-z, A-Z, 0-9. Digits 0 and 1 are slightly overrepresented (2/64 vs 1/64) due to padding the 62-character alphabet to a power of two.

func Testing added in v1.21.0

func Testing() (testFuncName string, underTest bool)

Testing indicates if the code is running inside a unit test, and if so, the test function name as well.

func ToKebabCase

func ToKebabCase(id string) string

ToKebabCase converts a CamelCase identifier to kebab-case. Consecutive non-letters or numbers are compressed into a single hyphen.

func ToSnakeCase

func ToSnakeCase(id string) string

ToSnakeCase converts a CamelCase identifier to snake_case. Consecutive non-letters are compressed into a single underscore.

func UnsafeBytesToString added in v1.13.1

func UnsafeBytesToString(b []byte) string

UnsafeBytesToString converts a slice of bytes to a string with no memory allocation. The original byte slice data should not be modified.

func UnsafeStringToBytes added in v1.13.1

func UnsafeStringToBytes(s string) []byte

UnsafeStringToBytes converts a string to a slice of bytes with no memory allocation. The slice points to the original data of the string and should not be modified.

Types

type SyncMap

type SyncMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

SyncMap is a map protected by a mutex.

func (*SyncMap[K, V]) Delete

func (sm *SyncMap[K, V]) Delete(key K) (value V, deleted bool)

Delete deletes the value for a key.

func (*SyncMap[K, V]) DoUnderLock added in v1.19.0

func (sm *SyncMap[K, V]) DoUnderLock(callback func(m map[K]V))

DoUnderLock obtains a lock and passes the internal map to the callback.

func (*SyncMap[K, V]) Keys added in v1.19.0

func (sm *SyncMap[K, V]) Keys() (keys []K)

Keys returns all keys.

func (*SyncMap[K, V]) Load

func (sm *SyncMap[K, V]) Load(key K) (value V, ok bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*SyncMap[K, V]) LoadOrStore

func (sm *SyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*SyncMap[K, V]) LoadOrStoreFunc added in v1.14.0

func (sm *SyncMap[K, V]) LoadOrStoreFunc(key K, value func() V) (actual V, loaded bool)

LoadOrStoreFunc returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*SyncMap[K, V]) Snapshot added in v1.19.0

func (sm *SyncMap[K, V]) Snapshot() (copy map[K]V)

Snapshot returns a shallow copy of the internal map.

func (*SyncMap[K, V]) Store

func (sm *SyncMap[K, V]) Store(key K, value V)

Store sets the value for a key.

func (*SyncMap[K, V]) Values added in v1.19.0

func (sm *SyncMap[K, V]) Values() (values []V)

Values returns all values.

Jump to

Keyboard shortcuts

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