utils

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2021 License: MIT Imports: 9 Imported by: 13

README

Utils

A Set of useful functions for working with files, errors and strings.

Error

TaggedError

Generate a tagged error.

// Signature:
TaggedError(tags []string, format string, args ...interface{}) error

// Example:
TaggedError([]string{"MyLib","MyMethod"}, "failed on %s file!", "main.json")
// [MyLib] [MyMethod] failed on main.json file!
IsErrorOf

Check if error has tag.

// Signature:
IsErrorOf(tag string, err error) bool

// Example:
IsErrorOf("MyLib", err) // true
HasError

Check if error is nil or not.

// Signature:
HasError(err error) bool
PanicOnError

Generate panic from error if error not nil.

// Signature:
PanicOnError(err error)
Handle Functions Return Value

This set of functions help you to handle output of function that have a return type with error value. this functions generate panic on error or return value. You can use this functions instead of if else block for checking error.

// Signature:
UIntOrPanic(res uint, err error) uint
UInt8OrPanic(res uint8, err error) uint8
UInt16OrPanic(res uint16, err error) uint16
UInt32OrPanic(res uint32, err error) uint32
UInt64OrPanic(res uint64, err error) uint64
IntOrPanic(res int, err error) int
Int8OrPanic(res int8, err error) int8
Int16OrPanic(res int16, err error) int16
Int32OrPanic(res int32, err error) int32
Int64OrPanic(res int64, err error) int64
Float32OrPanic(res float32, err error) float32
Float64OrPanic(res float64, err error) float64
StringOrPanic(res string, err error) string
BoolOrPanic(res bool, err error) bool
InterfaceOrPanic(res interface{}, err error) interface{}

// Example:
import "github.com/bopher/utils"
func TestIt(a int, b int) (int, error) { ... }
res = utils.IntOrPanic(TestIt(1, 3))

File

FileExists

Check if file exists or not.

// Signature:
FileExists(path string) (bool, error)

// Example:
import "github.com/bopher/utils"
exists, err := utils.FileExists("path/to/file")
IsDirectory

Check if path is a directory.

// Signature:
IsDirectory(path string) (bool, error)

// Example
import "github.com/bopher/utils"
ok, err := utils.IsDirectory("path/to/dir")
FindFile

Search for files in directory by a regex pattern.

// Signature:
FindFile(dir string, pattern string) []string

// Example:
import "github.com/bopher/utils"
files := utils.FindFile("path/to/dir", ".+\.sql") // => Get All file with sql extension
ClearDirectory

Delete all files and sub-directory in directory.

// Signature:
ClearDirectory(dir string) error
GetSubDirectory

Get list of sub directories.

// Signature:
GetSubDirectory(dir string) ([]string, error)
CreateDirectory

Create nested directory.

// Signature:
CreateDirectory(path string) error

// Example:
import "github.com/bopher/utils"
err := utils.CreateDirectory("a/b/c/d") // => Create all a, b, c and d directory

String

ExtractNumbers

Extract numbers from string.

import "github.com/bopher/utils"
numbers := utils.ExtractNumbers("(+1) 234-56789") // => 123456789
RandomStringFromCharset

Generate random string from character list.

import "github.com/bopher/utils"
str, err := utils.RandomStringFromCharset(5, "1234567890") // => "59102"
str2, err2 := utils.RandomStringFromCharset(3, "ABCDEFGH") // => "DFC"
RandomString

Generate random string from Alpha-Num Chars

import "github.com/bopher/utils"
str, err := utils.RandomString(5) // => "AB5S2"
Slugify

Generate dash separated string.

import "github.com/bopher/utils"
str := utils.Slugify("welcome to", "my site") // => "welcome-to-my-site"
ConcatStr

Join strings with separator.

// Signature:
ConcatStr(sep string, str ...string) string

// Example:
import "github.com/bopher/utils"
str := utils.ConcatStr(" ", "John", "", "Doe") // => "John Doe"
FormatNumber

Format number with comma separator.

import "github.com/bopher/utils"
func FormatNumber(format string, v ...interface{}) string {
str := utils.FormatNumber("$ %d [total] $ %d [remain]", 10000, 2500) // => "$ 10,000 [total] $ 2,500 [remain]"

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolOrPanic

func BoolOrPanic(res bool, err error) bool

BoolOrPanic get function result (bool, error) if error is not null generate panic otherwise return result

func ClearDirectory

func ClearDirectory(dir string) error

ClearDirectory delete all files and sub-directory in directory

func ConcatStr added in v1.2.0

func ConcatStr(sep string, str ...string) string

ConcatStr join strings with separator

func CreateDirectory

func CreateDirectory(path string) error

CreateDirectory create nested directory

func ExtractNumbers

func ExtractNumbers(str string) string

ExtractNumbers extract numbers from string

func FileExists

func FileExists(path string) (bool, error)

FileExists check if file exists

func FindFile

func FindFile(dir string, pattern string) []string

FindFile find files in directory with pattern

func Float32OrPanic

func Float32OrPanic(res float32, err error) float32

Float32OrPanic get function result (float32, error) if error is not null generate panic otherwise return result

func Float64OrPanic

func Float64OrPanic(res float64, err error) float64

Float64OrPanic get function result (float64, error) if error is not null generate panic otherwise return result

func FormatNumber added in v1.3.0

func FormatNumber(format string, v ...interface{}) string

FormatNumber format number with comma separator

func GetSubDirectory

func GetSubDirectory(dir string) ([]string, error)

GetSubDirectory get list of sub directories

func HasError

func HasError(err error) bool

HasError return true if error not nil, otherwise return false

func Int16OrPanic

func Int16OrPanic(res int16, err error) int16

Int16OrPanic get function result (int16, error) if error is not null generate panic otherwise return result

func Int32OrPanic

func Int32OrPanic(res int32, err error) int32

Int32OrPanic get function result (int32, error) if error is not null generate panic otherwise return result

func Int64OrPanic

func Int64OrPanic(res int64, err error) int64

Int64OrPanic get function result (int64, error) if error is not null generate panic otherwise return result

func Int8OrPanic

func Int8OrPanic(res int8, err error) int8

Int8OrPanic get function result (int8, error) if error is not null generate panic otherwise return result

func IntOrPanic

func IntOrPanic(res int, err error) int

IntOrPanic get function result (int, error) if error is not null generate panic otherwise return result

func InterfaceOrPanic

func InterfaceOrPanic(res interface{}, err error) interface{}

InterfaceOrPanic get function result (interface{}, error) if error is not null generate panic otherwise return result

func IsDirectory

func IsDirectory(path string) (bool, error)

IsDirectory check if path is directory

func IsErrorOf added in v1.3.0

func IsErrorOf(tag string, err error) bool

IsErrorOf check if error has tag

func PanicOnError

func PanicOnError(err error)

PanicOnError generate panic if error is not null

func RandomString

func RandomString(n uint) (string, error)

RandomString generate random string from Alpha-Num Chars

func RandomStringFromCharset

func RandomStringFromCharset(n uint, letters string) (res string, err error)

RandomStringFromCharset generate random string from character list

func Slugify added in v1.2.0

func Slugify(str ...string) string

Slugify make slugify string

func StringOrPanic

func StringOrPanic(res string, err error) string

StringOrPanic get function result (string, error) if error is not null generate panic otherwise return result

func TaggedError added in v1.3.0

func TaggedError(tags []string, format string, args ...interface{}) error

TaggedError generate a tagged error

func UInt16OrPanic

func UInt16OrPanic(res uint16, err error) uint16

UInt16OrPanic get function result (uint16, error) if error is not null generate panic otherwise return result

func UInt32OrPanic

func UInt32OrPanic(res uint32, err error) uint32

UInt32OrPanic get function result (uint32, error) if error is not null generate panic otherwise return result

func UInt64OrPanic

func UInt64OrPanic(res uint64, err error) uint64

UInt64OrPanic get function result (uint64, error) if error is not null generate panic otherwise return result

func UInt8OrPanic

func UInt8OrPanic(res uint8, err error) uint8

UInt8OrPanic get function result (uint8, error) if error is not null generate panic otherwise return result

func UIntOrPanic

func UIntOrPanic(res uint, err error) uint

UIntOrPanic get function result (uint, error) if error is not null generate panic otherwise return result

Types

This section is empty.

Jump to

Keyboard shortcuts

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