util

package
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2022 License: Apache-2.0 Imports: 12 Imported by: 17

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 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 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 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 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 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 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