util

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2023 License: Apache-2.0 Imports: 14 Imported by: 7

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CastSlice added in v0.2.0

func CastSlice[T any](vals []interface{}) []T

CastSlice casts a slice of interface{} to specific type T.

func CdBack added in v0.1.16

func CdBack(backDir string)

CdBack returns back to previous path.

func CdToThis

func CdToThis()

CdToThis changes `working directory` to the current directory of function that calls it.

A use case is when a function at coding file reads a relative file, however the end user, at run time, executes the coding file at a different directory to the coding file. As relative path has been used, one would change from any directory at run time to `current` directory where function is called to get the correct relative path.

Example: There a Go package in a os directory as follow:

  • os-home | +--go-package | | | +--data | | | | | +--data-file.txt | | | +--sub-package | | | +--go-file.go | +--other-folder

There's a function `ReadData` in `go-file.go` that read a data file `data-file.txt` in other directory using a relative filepath to its - I.e: "../data/data-file.txt". Now, a end user run `go-file.go` from `other-folder` directory which is not at the same directory to `go-file.go` file. `ReadData` function would be panic if there was no helper to change directory from directory where end user executes the code ("os-home/other-folder") to directory of `go-file.go`. `CdToThis` does this job.

func Contains added in v0.2.0

func Contains(items []string, item string) bool

func ConvertSlice added in v0.2.0

func ConvertSlice[M, N Convertable](vals []M) []N

func DeepCopy added in v0.2.0

func DeepCopy(src, dist interface{}) (err error)

func ErrorContains

func ErrorContains(out error, want string) bool

ErrorContains checks if the error message in out contains the text in want Ref. https://stackoverflow.com/questions/42035104

Example usage if !ErrorContains(err, "unexpected banana") { t.Errorf("unexpected error: %v", err) }

if !ErrorContains(err, "") { t.Errorf("unexpected error: %v", err) }

func FileSize

func FileSize(filepath string) (int64, error)

FileSize returns length of given file using `os.Stat()` Ref. https://stackoverflow.com/questions/17133590

func GetType added in v0.2.0

func GetType(myvar interface{}) string

func IsNil added in v0.2.0

func IsNil(i interface{}) bool

IsNil checks whether is a variable of type interface{} is nil.

func LogError

func LogError(err error)

LogError logs error with the function name as well.

func MakeRange

func MakeRange(start, end int) []int

* // Makerange creates a sequence of number (range) * // Ref. https://stackoverflow.com/questions/39868029 * func MakeRange(min, max int) []int { * if min == max { * return []int{} * } * a := make([]int, max-min+1) * for i := range a { * a[i] = min + i * } * return a * } *

func Merge added in v0.2.0

func Merge[T any](a, b []T) []T

Merge merges 2 slices to a new slice. It ensures that the return value is a new slice but not an appended of slice `a` as in append(a, b...).

func MinMax

func MinMax(array []int) (int, int)

MinMax returns min and max from input int array

func MinMaxFloat64

func MinMaxFloat64(array []float64) (float64, float64)

MinMax returns min and max from input int array

func ReadAllLn

func ReadAllLn(filepath string, keepBreakLine bool) ([]string, error)

ReadAllLn reads all line by line from a file using bufio.scanner

func Repeat added in v0.2.0

func Repeat[T RepeatType](item T, length int) []T

func StringInSlice

func StringInSlice(a string, list []string) bool

StringInSlice check whether given string is in a slice

func StringIndex

func StringIndex(s, sub string) (index int, err error)

StringIndex returns index (start) for substring on a given string It returns -1 and error if not matching

func ToASCII

func ToASCII(s string) string

ToASCII converts string to ASCII form Ref. https://stackoverflow.com/questions/12668681

func ToGrapheme

func ToGrapheme(s string) string

ToGrapheme converts string to grapheme TODO: should we include this func? Ref: https://github.com/golang/go/issues/14820

func TraceError

func TraceError(err error) error

HandleError wraps original error message with function and source code position where error is captured. Ref. https://stackoverflow.com/questions/24809287

func Zip

func Zip(a, b, c interface{}) error

Zip zips 2 slices in first and second arguments to third argument Ref: https://stackoverflow.com/questions/26957040

Usage Example a := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0} b := []int{0, 9, 8, 7, 6, 5, 4, 3, 2, 1} c := [][2]int{}

e := zip(a, b, &c)

if e != nil {
		fmt.Println(e)
 	return
}

fmt.Println(c)

Types

type Convertable added in v0.2.0

type Convertable interface {
	int | int64 | float64
}

type Iter

type Iter struct {
	// contains filtered or unexported fields
}

Iter contains data and methods for an interator

func NewIter

func NewIter(data interface{}) *Iter

NewIter creates a Iter from an input slice. Otherwise it will panic.

func (*Iter) Next

func (it *Iter) Next() (retVal interface{}, ok bool)

Next implements a iterator functionality for Iter

type ParamOption added in v0.2.0

type ParamOption func(*Params)

func WithParam added in v0.2.0

func WithParam(key string, value interface{}) ParamOption

WithParam adds a parameter option to Params.

func WithParams added in v0.2.0

func WithParams(p *Params) []ParamOption

type Params added in v0.2.0

type Params struct {
	// contains filtered or unexported fields
}

Params constructs function parameters similar to Python **kwargs. It is used to add multiple optional parameters to a function.

func DefaultParams added in v0.2.0

func DefaultParams() *Params

DefaultParams creates Params with default values.

func NewParams added in v0.2.0

func NewParams(values map[string]interface{}) *Params

NewParams creates a new Params.

func (*Params) Clone added in v0.2.0

func (p *Params) Clone() *Params

Clone clones (deep copy) all parameters to new Params.

func (*Params) Copy added in v0.2.0

func (p *Params) Copy(params *Params, key string, newKeyOpt ...string)

Copy (shallow) copies parameter from one Params to other Params

func (*Params) DeepCopy added in v0.2.0

func (p *Params) DeepCopy(params *Params, key string, newKeyOpt ...string)

DeepCopy copies a param with given name.

func (*Params) Delete added in v0.2.0

func (p *Params) Delete(key string)

func (*Params) DeleteAll added in v0.2.0

func (p *Params) DeleteAll()

func (*Params) Get added in v0.2.0

func (p *Params) Get(key string, defaultValueOpt ...interface{}) (val interface{})

Get returns a parameter value by its key. If returns nil/default value if parameter type or value is nil.

func (*Params) Has added in v0.2.0

func (p *Params) Has(key string) bool

Has returns whether having a not-nil value corresponding to given key.

func (*Params) Keys added in v0.2.0

func (p *Params) Keys() []string

Keys returns a slice of parameter names.

func (*Params) Len added in v0.2.0

func (p *Params) Len() int

Len returns number of parameters.

func (*Params) Param added in v0.2.0

func (p *Params) Param(key string) (value interface{})

Param returns value of parameter corresponding to key. It returns nil if not found.

func (*Params) Pop added in v0.2.0

func (p *Params) Pop(key string) (value interface{})

Pop pops(removes) and returns parameter with key from Params. It returns nil if value not found.

func (*Params) Select added in v0.2.0

func (p *Params) Select(keys []string) *Params

Select selects a subset of parameters

func (*Params) Set added in v0.2.0

func (p *Params) Set(key string, value interface{})

Set adds/updates a new parameter to Params.

func (*Params) Values added in v0.2.0

func (p *Params) Values() map[string]interface{}

Values returns a map of parameters.

type RepeatType added in v0.2.0

type RepeatType interface{ int | string | []int }

type RuneGen

type RuneGen func() rune

Ref. https://stackoverflow.com/questions/14000534

func MapRune

func MapRune(g RuneGen, f func(rune) rune) RuneGen

MapRune maps ...

type RuneIter

type RuneIter struct {
	// contains filtered or unexported fields
}

RuneIter is rune iterator with Next() method.

func NewRuneIter

func NewRuneIter(data []rune) *RuneIter

NewRuneIter creates a RuneIter

func (*RuneIter) CurrentIndex

func (it *RuneIter) CurrentIndex() int

CurrentIndex returns current index of RuneIter

func (*RuneIter) Len

func (it *RuneIter) Len() int

Len returns total items in RuneIter

func (*RuneIter) Next

func (it *RuneIter) Next() (retVal rune, ok bool)

Next implement interator for RuneIter

func (*RuneIter) Reset

func (it *RuneIter) Reset()

Reset resets current index to first item if any.

type RuneReader

type RuneReader struct {
	// contains filtered or unexported fields
}

func NewRuneReader

func NewRuneReader(r []rune) RuneReader

NewRuneReader create a new of type `io.RuneReader`

func (*RuneReader) ReadRune

func (r *RuneReader) ReadRune() (rune, int, error)

ReadRune implements `io.RuneReader` for RuneReader struct

Directories

Path Synopsis
utils slice manupulation Ref.
utils slice manupulation Ref.

Jump to

Keyboard shortcuts

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