utilities

package module
v0.1.16 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: Apache-2.0 Imports: 18 Imported by: 4

README

Utilities

Go Report Card godoc

Everybody has little functions that the find themselves writing ... and re-writing in different packages. Enough! I got tired of that, so I created a package for it.

You might find them useful, too.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any2Date added in v0.0.9

func Any2Date(inVal any) (*time.Time, error)

Any2Date attempts to convert inVal to a date (time.Time). Returns nil if this fails.

func Any2Float32 added in v0.0.9

func Any2Float32(inVal any) (*float32, error)

Any2Float32 attempts to convert inVal to float32. Returns nil if this fails.

func Any2Float64 added in v0.0.9

func Any2Float64(inVal any) (*float64, error)

Any2Float64 attempts to convert inVal to float64. Returns nil if this fails.

func Any2Int added in v0.0.9

func Any2Int(inVal any) (*int, error)

Any2Int attempts to convert inVal to int. Returns nil if this fails.

func Any2Int32 added in v0.0.9

func Any2Int32(inVal any) (*int32, error)

Any2Int32 attempts to convert inVal to int32. Returns nil if this fails.

func Any2Int64 added in v0.0.9

func Any2Int64(inVal any) (*int64, error)

Any2Int64 attempts to convert inVal to int64. Returns nil if this fails.

func Any2Kind added in v0.0.9

func Any2Kind(inVal any, kind reflect.Kind) (any, error)

func Any2String added in v0.0.9

func Any2String(inVal any) string

func AnySlice2Float64 added in v0.0.12

func AnySlice2Float64(inVal []any) ([]float64, error)

func BuildQuery

func BuildQuery(srcQry string, replacers keyval.KeyVal) (qry string)

BuildQuery replaces the placeholders with values placeholders have the form "?key". BuildQuery prepends a "?" to the keys in replacers.

func Comparer added in v0.0.9

func Comparer(xa, xb any, comp string) (truth bool, err error)

Comparer compares xa and xb. Comparisons available are: ==, !=, >, <, >=, <=

func CopyFile added in v0.0.6

func CopyFile(sourceFile, destFile string) error

CopyFile copies sourceFile to destFile

func CopyFiles added in v0.0.6

func CopyFiles(fromDir, toDir string) error

CopyFiles recursively copies files from fromDir to toDir

func DBExists added in v0.0.11

func DBExists(db string, conn *chutils.Connect) error

DBExists returns an error if db does not exist

func DropTable added in v0.0.5

func DropTable(table string, conn *chutils.Connect) error

DropTable drops the table from ClickHouse

func Fig2File added in v0.1.16

func Fig2File(fig *grob.Fig, outDir, outFile string) error

Fig2File outputs a plotly figure to a graphics file (png, jpg, etc) This package requires that orca be installed. The orca repo is [here](https://github.com/plotly/orca).

Steps:

  1. Download the latest [release](https://github.com/plotly/orca/releases) of the AppImage.

  2. Put it somewhere safe.

  3. chmod +x

  4. make a symbolic link called "orca" in your path.

    orca requires FUSE. FUSE installation instructions are [here](https://github.com/AppImage/AppImageKit/wiki/FUSE)

Note that --no-sandbox is added to orca per this [thread](https://github.com/chrismaltby/gb-studio/issues/1102).

func FileExists

func FileExists(file string) error

FileExists returns an error if "file" does not exist.

func GTAny added in v0.0.9

func GTAny(xa, xb any) (truth bool, err error)

GTAny compares xa > xb

func Has

func Has(needle, delim string, haystack ...string) bool

Has returns true if needle is in haystack

func LTAny added in v0.0.10

func LTAny(x, y any) (bool, error)

LTAny returns x<y for select underlying types of "any"

func MaxInt

func MaxInt(ints ...int) int

MaxInt returns the maximum of ints

func MinInt

func MinInt(ints ...int) int

MinInt returns the minimum of ints

func Position

func Position(needle, delim string, haystack ...string) int

Position returns the index of needle in the haystack. It returns -1 if needle is not found. If haystack has length 1, then it is split into a slice with delimiter ",".

func PrettyDur added in v0.0.12

func PrettyDur(startTime time.Time) string

PrettyDur returns a run duration in a minutes/seconds format

func RandNorm added in v0.0.6

func RandNorm(n int) ([]float64, error)

RandNorm generates a slice whose elements are N(0,1)

func RandUnifFlt added in v0.0.6

func RandUnifFlt(n int) ([]float64, error)

RandUnifFlt generates a slice whose elements are random U(0, 1) floats

func RandUnifInt added in v0.0.6

func RandUnifInt(n, upper int) ([]int64, error)

RandUnifInt generates a slice whose elements are random U[0,upper) int64's

func RandomLetters added in v0.0.7

func RandomLetters(length int) string

RandomLetters generates a string of length "length" by randomly choosing from a-z

func ReplaceSmart added in v0.0.12

func ReplaceSmart(source, oldChar, newChar, delim string) string

ReplaceSmart replaces old with new except when it occurs within delim

func Slash

func Slash(inStr string) string

Slash adds a trailing slash if inStr doesn't end in a slash

func String2Kind added in v0.0.9

func String2Kind(str string) reflect.Kind

String2Kind converts a string specifying a type to the reflect.Kind

func TableExists

func TableExists(table string, conn *chutils.Connect) error

TableExists returns an error if "table" does not exist. conn is the DB connector.

func TableOrQuery added in v0.0.5

func TableOrQuery(table string) string

TableOrQuery takes table and returns a query. If it is already a query (has "select"), just returns the original value.

func TempFile

func TempFile(ext string, length int) string

TempFile produces a random temp file name in the system's tmp location. The file has extension "ext". The file name begins with "tmp" has length 3 + length.

func TempTable

func TempTable(tmpDB string, length int) string

TempTable produces a random table name. The table name begins with "tmp". The table's name has length 3 +length. tmpDB is the database name.

func ToFile

func ToFile(fileName, text string) error

ToFile writes string to file fileName, which is created

func ToLastDay added in v0.0.13

func ToLastDay(dt time.Time) (eom time.Time)

moves a date to the last day of the month

func YesNo added in v0.0.12

func YesNo(inStr string) (bool, error)

YesNo determines if inStr is yes/no, return true if "yes"

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳