goutils

package module
v0.0.0-...-d7e01b7 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: MIT Imports: 7 Imported by: 0

README

goutils

Golang utils

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnyAppendIfNotExists

func AnyAppendIfNotExists[T any](slice []T, newItem T, checkExists func(T) bool) []T

AnyAppendIfNotExists Append an element to a slice only if it isn’t existed Usage:

	type Thing struct {
  ID int
  Name string
	}
	var things = []Thing{
	{ID: 1, Name: "one"},
	{ID: 2, Name: "two"},
	}
	var thing2 = Thing{ID: 2, Name: "Thing 2"}
	var thingSlice1 = AnyAppendIfNotExists(things, thing2, func (element Thing) bool {
  return element.ID == thing2.ID
	})
	fmt.Println(len(thingSlice1)) // 2

func B2ds

func B2ds(v []byte) string

B2ds return a Digit string of v v (8-byte big endian) -> uint64(123456) -> "123456".

func B2i

func B2i(v []byte) uint64

B2i return an int64 of v v (8-byte big endian) -> uint64(123456).

func B2s

func B2s(b []byte) string

B2s converts byte slice to a string without memory allocation. []byte("abc") -> "abc" s

func Bconcat

func Bconcat(slices [][]byte) []byte

Bconcat concat a list of byte

func Bconcat2

func Bconcat2(slices ...[]byte) []byte

Bconcat2 concat a list of byte

func CloneBytes

func CloneBytes(src []byte) []byte

func CmdExists

func CmdExists(cmd string) bool

CmdExists Golang check if command exists

func ComparableAppendIfNotExists

func ComparableAppendIfNotExists[T comparable](slice []T, newItem T) []T

ComparableAppendIfNotExists Append an element to a slice only if it isn’t existed Usage:

var intSlice = []int{1, 2, 3}
var intSlice1 = ComparableAppendIfNotExists(intSlice, 3)
var intSlice2 = ComparableAppendIfNotExists(intSlice, 4)
fmt.Println(intSlice1) // []int{1, 2, 3}
fmt.Println(intSlice2) // []int{1, 2, 3, 4}

func ComparableContains

func ComparableContains[T comparable](slice []T, element T) bool

ComparableContains Check if a slice contain an element Usage:

var stringSlice = []string{"item 1", "item 2"}
var intSlice = []int{1, 2, 3}

fmt.Println(ComparableContains(stringSlice, "item 2")) // true
fmt.Println(ComparableContains(intSlice, 4)) // false

func ComparableFilter

func ComparableFilter[T comparable](slice []T, predicate func(T) bool) []T

ComparableFilter Filter a slice to return only the elements that match a condition Usage:

	var intSlice = []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	var oddSlice = ComparableFilter(intSlice, func(element int) bool {
   return element % 2 != 0
	})
	fmt.Println(oddSlice) // []int{1, 3, 5, 7, 9}

func ComparableMap

func ComparableMap[T comparable, R comparable](slice []T, mapper func(T) R) []R

ComparableMap Transform a slice into a difference slice type Usage:

	var intSlice = []int{1, 2, 3}
	var strSlice = ComparableMap(intSlice, func(element int) string {
   return strconv.Itoa(element)
	})
	fmt.Println(strSlice) // []string{"1", "2", "3"}

func ComparableOverlap

func ComparableOverlap[T comparable](slice1 []T, slice2 []T) []T

ComparableOverlap Get the overlap elements between two slice Usage:

var slice1 = []int{1, 2, 3}
var slice2 = []int{2, 3, 4}
var result = ComparableOverlap(slice1, slice2)
fmt.Println(result) // []int{2, 3}

func ComparableRepeat

func ComparableRepeat[T comparable](input T, times int) []T

ComparableRepeat Create a slice with a repeated values Usage:

var intSlice = ComparableRepeat(1, 3)
fmt.Println(intSlice) // []int{1, 1, 1}

func DS2b

func DS2b(v string) []byte

DS2b returns an 8-byte big endian representation of Digit string v ("123456") -> uint64(123456) -> 8-byte big endian.

func DS2i

func DS2i(v string) uint64

DS2i returns uint64 of Digit string v ("123456") -> uint64(123456).

func ExtendByteSlice

func ExtendByteSlice(b []byte, needLen int) []byte

ExtendByteSlice extends b to needLen bytes

func FormatInt

func FormatInt(number int64) string

func I2b

func I2b(v uint64) []byte

I2b returns an 8-byte big endian representation of v v uint64(123456) -> 8-byte big endian.

func RandBytes

func RandBytes(dst []byte) []byte

RandBytes returns dst with a string random bytes Make sure that dst has the length you need.

func S2b

func S2b(s string) []byte

S2b converts string to a byte slice without memory allocation. "abc" -> []byte("abc")

func SliceUniqInt

func SliceUniqInt(s []int) []int

func SliceUniqStr

func SliceUniqStr(s []string) []string

func StringSliceInclude

func StringSliceInclude(vs []string, t string) bool

StringSliceInclude returns true or false if given string is in slice

func StringSliceIndexOf

func StringSliceIndexOf(vs []string, s string) int

StringSliceIndexOf returns index position in slice from given string If value is -1, the string does not found

Types

This section is empty.

Directories

Path Synopsis
tsmap
Package tsmap provides a collection of thread-safe map functions that can be safely used between multiple goroutines.
Package tsmap provides a collection of thread-safe map functions that can be safely used between multiple goroutines.
tsslice
Package tsslice provides a collection of thread-safe slice functions that can be safely used between multiple goroutines.
Package tsslice provides a collection of thread-safe slice functions that can be safely used between multiple goroutines.
trans

Jump to

Keyboard shortcuts

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