xo

package module
v1.18.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 27 Imported by: 17

README

xo

Go Reference

🪐 Universal external Golang utilities, implementations, and even experimental coding patterns, designs

Development

Clone the repository:

git clone https://github.com/nekomeowww/xo
cd xo

Prepare the dependencies:

go mod tidy

[!NOTE] If you want to work with protobufs/testpb directory and generate new Golang code, you need to install the buf tool.

cd protobufs
buf dep update
buf generate --path ./testpb

🤠 Spec

GoDoc: https://godoc.org/github.com/nekomeowww/xo

👪 Other family members of anyo

Documentation

Index

Constants

View Source
const (
	UnitBytesOfTB = 1000 * UnitBytesOfGB
	UnitBytesOfGB = 1000 * UnitBytesOfMB
	UnitBytesOfMB = 1000 * UnitBytesOfKB
	UnitBytesOfKB = 1000

	UnitBytesOfTiB = 1024 * UnitBytesOfGiB
	UnitBytesOfGiB = 1024 * UnitBytesOfMiB
	UnitBytesOfMiB = 1024 * UnitBytesOfKiB
	UnitBytesOfKiB = 1024
)

Variables

View Source
var (
	UnitSecondsOfMonth  = 30 * UnitSecondsOfDay
	UnitSecondsOfDay    = 24 * UnitSecondsOfHour
	UnitSecondsOfHour   = 60 * UnitSecondsOfMinute
	UnitSecondsOfMinute = 60 * time.Second
)

Functions

func Clone added in v1.15.1

func Clone[T any, Slice ~[]T](collection Slice) Slice

Clone returns a new slice contains items cloned from collection.

func ContainsCJKChar

func ContainsCJKChar(s string) bool

ContainsCJKChar determines whether a string contains CJK characters.

func ConvertUint64ToDecimalString

func ConvertUint64ToDecimalString(amount uint64, prec int) string

ConvertUint64ToDecimalString formats a uint64 to a string with a decimal point.

func DigitOf

func DigitOf[I uint | uint16 | uint32 | uint64 | int | int16 | int32 | int64](val I, n int) I

DigitOf returns the n-th digit of val.

Underlying calculation is based on (val % 10^n) .

func FindDuplicates

func FindDuplicates[T comparable](a []T) []T

FindDuplicates returns a new slice contains items that are duplicated in a.

func FromPtrAny added in v1.9.7

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

FromPtrAny returns the value from the given pointer.

func FromString added in v1.9.7

func FromString[T any](str string) (T, error)

func FromStringOrEmpty added in v1.9.7

func FromStringOrEmpty[T any](str string) T

func Intersection

func Intersection[T comparable](a, b []T) []T

Intersection returns a new slice contains items that are in both a and b.

func IsASCIIPrintable

func IsASCIIPrintable(s string) bool

IsASCIIPrintable determines whether a string is printable ASCII.

func IsDecimalsPlacesValid

func IsDecimalsPlacesValid(num float64, decimalPlaces int) bool

func IsInTestEnvironment

func IsInTestEnvironment() bool

IsInTestEnvironment determines whether the current environment is a test environment.

func IsJSON

func IsJSON(str string) bool

IsJSON determines whether the string is JSON.

func IsJSONBytes

func IsJSONBytes(bytes []byte) bool

IsJSONBytes determines whether the bytes is JSON.

func IsStringPrintable

func IsStringPrintable(str string) bool

IsStringPrintable determines whether a string is printable.

func IsValidUUID

func IsValidUUID(uuidStr string) bool

IsValidUUID determines whether a string is a valid UUID.

func Join

func Join[T any](from []T, sep string) string

Join returns a string contains items joined with sep by using fmt.Sprintf.

func JoinBy added in v1.9.7

func JoinBy[T any](from []T, sep string, convertFunc func(item T) string) string

JoinBy returns a string contains converted items joined with sep.

func JoinWithConverter deprecated

func JoinWithConverter[T any](from []T, sep string, convertFunc func(item T) string) string

JoinWithConverter returns a string contains converted items joined with sep.

Deprecated: Use JoinBy instead.

func MapString added in v1.9.7

func MapString[T any](from []T, sep string) []string

MapString returns a new slice contains stringified items.

func MapStringBy added in v1.9.7

func MapStringBy[T any](from []T, sep string, convertFunc func(item T) string) []string

MapStringBy returns a new slice contains converted items.

func NormalizeAsRFC1123Name added in v1.9.7

func NormalizeAsRFC1123Name(value string) string

func ParseUint64FromDecimalString

func ParseUint64FromDecimalString(decimalStr string, percision int) (uint64, error)

ParseUint64FromDecimalString converts a string with a decimal point to a uint64.

func Print

func Print(inputs ...interface{})

Print formats the output of all incoming values in terms of field, value, type, and size.

func PrintJSON

func PrintJSON(inputs ...interface{})

PrintJSON formats the output of all incoming values in JSON format.

func Race2 added in v1.18.0

func Race2[T0 any, T1 any](src1 chan T0, src2 chan T1) (T0, T1)

func Race3 added in v1.18.0

func Race3[T0 any, T1 any, T2 any](src1 chan T0, src2 chan T1, src3 chan T2) (T0, T1, T2)

func Race4 added in v1.18.0

func Race4[T0 any, T1 any, T2 any, T3 any](src1 chan T0, src2 chan T1, src3 chan T2, src4 chan T3) (T0, T1, T2, T3)

func Race5 added in v1.18.0

func Race5[T0 any, T1 any, T2 any, T3 any, T4 any](src1 chan T0, src2 chan T1, src3 chan T2, src4 chan T3, src5 chan T4) (T0, T1, T2, T3, T4)

func Race6 added in v1.18.0

func Race6[T0 any, T1 any, T2 any, T3 any, T4 any, T5 any](src1 chan T0, src2 chan T1, src3 chan T2, src4 chan T3, src5 chan T4, src6 chan T5) (T0, T1, T2, T3, T4, T5)

func RandBytes

func RandBytes(length ...int) ([]byte, error)

RandBytes generates bytes according to the given length, defaults to 32.

func RandomBase64Token

func RandomBase64Token(length ...int) (string, error)

RandomBase64Token generates the URL-safe Base64 string based on the given byte length, the length is 32 by default, the length is the length of the original byte data, not the actual length of the Base64 string, the actual length is about 44 by default in the case of 32.

func RandomHashString

func RandomHashString(length ...int) string

RandomHashString generates a random SHA256 string with the maximum length of 64.

func RandomInt64

func RandomInt64(maxVal ...int64) int64

RandomInt64 generates a random integer.

func RandomInt64InRange

func RandomInt64InRange(minVal, maxVal int64) int64

RandomInt64InRange generates a random integer in the range.

func RandomInt64String

func RandomInt64String(digits int64) string

RandomInt64String generates a random integer string.

func ReadCSV added in v1.5.0

func ReadCSV(path string) ([][]string, error)

func ReadFileAsBytesBuffer

func ReadFileAsBytesBuffer(path string) (*bytes.Buffer, error)

ReadFileAsBytesBuffer reads a file and returns a bytes.Buffer.

func RelativePathBasedOnPwdOf

func RelativePathBasedOnPwdOf(fp string) string

func RelativePathOf

func RelativePathOf(fp string) string

func SliceSlices

func SliceSlices[T any](from []T, each int) [][]T

SliceSlices returns a new slice contains slices with maximum length each.

func Sprint

func Sprint(inputs ...interface{}) string

Sprint formats the output of all the fields, values, types, and sizes of the values passed in and returns the string.

NOTICE: newline control character is included.

func SprintJSON

func SprintJSON(inputs ...interface{}) string

SprintJSON formats the output of all incoming values in JSON format and

NOTICE: newline control character is included.

func Stringify added in v1.9.7

func Stringify(v any) string

func Substring added in v1.9.7

func Substring(str string, start, end int) string

func Tee added in v1.18.0

func Tee[T any](src chan T) (chan T, chan T)

func ToMap

func ToMap[T any, K comparable](t []T, keyGetter func(T) K) map[K]T

ToMap converts a slice to a map with key from key getter func and pairs with value.

func ToPtrAny added in v1.9.7

func ToPtrAny(v any) *any

ToPtrAny returns a pointer to the given value.

Types

type EmptyIoReader added in v1.3.0

type EmptyIoReader = NopIoReader

type EmptyIoWriter added in v1.3.0

type EmptyIoWriter = NopIoWriter

type NopIoReader added in v1.3.0

type NopIoReader struct{}

func NewEmptyIoReader added in v1.3.0

func NewEmptyIoReader() *NopIoReader

func NewNopIoReader added in v1.3.0

func NewNopIoReader() *NopIoReader

func (*NopIoReader) Read added in v1.3.0

func (r *NopIoReader) Read(p []byte) (n int, err error)

type NopIoWriter added in v1.3.0

type NopIoWriter struct{}

func NewEmptyIoWriter added in v1.3.0

func NewEmptyIoWriter() *NopIoWriter

func NewNopIoWriter added in v1.3.0

func NewNopIoWriter() *NopIoWriter

func (*NopIoWriter) Write added in v1.3.0

func (w *NopIoWriter) Write(p []byte) (n int, err error)

type ProtoValueScanner added in v1.9.7

type ProtoValueScanner[T any] struct {
}

ProtoValueScanner is a field.ValueScanner that implements the ent.ValueScanner interface as helper for working with protobuf messages. It is used to scan and convert protobuf messages to and from the database.

func (SomeTable) Fields() []ent.Field {
	return []ent.Field{
		field.
		String("payload").
		ValueScanner(utils.ProtoValueScanner[somepb.YourMessage]{}).
		GoType(&somepb.YourMessage{}).
		SchemaType(map[string]string{
			dialect.Postgres: "jsonb",
			dialect.MySQL:    "json",
			dialect.SQLite:   "json",
		}),
	}
}

func (ProtoValueScanner[T]) FromValue added in v1.9.7

func (s ProtoValueScanner[T]) FromValue(value driver.Value) (vt *T, err error)

FromValue returns the field instance from the ScanValue above after the database value was scanned.

func (ProtoValueScanner[T]) ScanValue added in v1.9.7

func (s ProtoValueScanner[T]) ScanValue() field.ValueScanner

ScanValue returns a new ValueScanner that functions as an intermediate result between database value and GoType value. For example, sql.NullString or sql.NullInt.

func (ProtoValueScanner[T]) Value added in v1.9.7

func (s ProtoValueScanner[T]) Value(data *T) (driver.Value, error)

Value returns the driver.Valuer for the GoType.

Directories

Path Synopsis
exp
Package logger
Package logger
protobufs

Jump to

Keyboard shortcuts

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