Documentation ¶
Index ¶
- func CastSlice[T any](vals []interface{}) []T
- func CdBack(backDir string)
- func CdToThis()
- func Contains(items []string, item string) bool
- func ConvertSlice[M, N Convertable](vals []M) []N
- func DeepCopy(src, dist interface{}) (err error)
- func ErrorContains(out error, want string) bool
- func FileSize(filepath string) (int64, error)
- func GetType(myvar interface{}) string
- func IsNil(i interface{}) bool
- func LogError(err error)
- func MakeRange(start, end int) []int
- func Merge[T any](a, b []T) []T
- func MinMax(array []int) (int, int)
- func MinMaxFloat64(array []float64) (float64, float64)
- func ReadAllLn(filepath string, keepBreakLine bool) ([]string, error)
- func Repeat[T RepeatType](item T, length int) []T
- func StringInSlice(a string, list []string) bool
- func StringIndex(s, sub string) (index int, err error)
- func ToASCII(s string) string
- func ToGrapheme(s string) string
- func TraceError(err error) error
- func Zip(a, b, c interface{}) error
- type Convertable
- type Iter
- type ParamOption
- type Params
- func (p *Params) Clone() *Params
- func (p *Params) Copy(params *Params, key string, newKeyOpt ...string)
- func (p *Params) DeepCopy(params *Params, key string, newKeyOpt ...string)
- func (p *Params) Delete(key string)
- func (p *Params) DeleteAll()
- func (p *Params) Get(key string, defaultValueOpt ...interface{}) (val interface{})
- func (p *Params) Has(key string) bool
- func (p *Params) Keys() []string
- func (p *Params) Len() int
- func (p *Params) Param(key string) (value interface{})
- func (p *Params) Pop(key string) (value interface{})
- func (p *Params) Select(keys []string) *Params
- func (p *Params) Set(key string, value interface{})
- func (p *Params) Values() map[string]interface{}
- type RepeatType
- type RuneGen
- type RuneIter
- type RuneReader
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CastSlice ¶
func CastSlice[T any](vals []interface{}) []T
CastSlice casts a slice of interface{} to specific type T.
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 ConvertSlice ¶
func ConvertSlice[M, N Convertable](vals []M) []N
func ErrorContains ¶
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 ¶
FileSize returns length of given file using `os.Stat()` Ref. https://stackoverflow.com/questions/17133590
func IsNil ¶
func IsNil(i interface{}) bool
IsNil checks whether is a variable of type interface{} is nil.
func MakeRange ¶
* // 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 ¶
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 MinMaxFloat64 ¶
MinMax returns min and max from input int array
func Repeat ¶
func Repeat[T RepeatType](item T, length int) []T
func StringInSlice ¶
StringInSlice check whether given string is in a slice
func StringIndex ¶
StringIndex returns index (start) for substring on a given string It returns -1 and error if not matching
func ToASCII ¶
ToASCII converts string to ASCII form Ref. https://stackoverflow.com/questions/12668681
func ToGrapheme ¶
ToGrapheme converts string to grapheme TODO: should we include this func? Ref: https://github.com/golang/go/issues/14820
func TraceError ¶
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 ¶
type Iter ¶
type Iter struct {
// contains filtered or unexported fields
}
Iter contains data and methods for an interator
type ParamOption ¶
type ParamOption func(*Params)
func WithParam ¶
func WithParam(key string, value interface{}) ParamOption
WithParam adds a parameter option to Params.
func WithParams ¶
func WithParams(p *Params) []ParamOption
type Params ¶
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 (*Params) Get ¶
Get returns a parameter value by its key. If returns nil/default value if parameter type or value is nil.
func (*Params) Param ¶
Param returns value of parameter corresponding to key. It returns nil if not found.
func (*Params) Pop ¶
Pop pops(removes) and returns parameter with key from Params. It returns nil if value not found.
type RepeatType ¶
type RuneIter ¶
type RuneIter struct {
// contains filtered or unexported fields
}
RuneIter is rune iterator with Next() method.
func (*RuneIter) CurrentIndex ¶
CurrentIndex returns current index of RuneIter
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`