utils

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2023 License: MIT Imports: 16 Imported by: 3

README

Golang Utilities

Installation
go get git.verzth.work/go/utils
Available Utils

Take a look on demo source.

package mypackage

import (
	"fmt"
	"git.verzth.work/go/utils"
)

func main() {
	fmt.Printf("RandString: %v\n", utils.RandString(20))
	fmt.Printf("RandStringWithCharset: %v\n", utils.RandStringWithCharset(20,"abc123efg456"))
	fmt.Printf("RandStringLower: %v\n", utils.RandStringLower(20))
	fmt.Printf("RandomHex: %v\n",utils.RandomHex(16))
	fmt.Printf("RandomHexUpper: %v\n",utils.RandomHexUpper(16))
	ranString := utils.RandStringUpper(10)
	fmt.Printf("RandStringUpper: %v\n",ranString) // 'ea10a9c919'

	fmt.Printf("Substring: %v\n", utils.Substring(ranString,2)) // '10a9c919'
	fmt.Printf("Substring: %v\n", utils.Substring(ranString,1,8)) // 'a10a9c9'
	fmt.Printf("Substring: %v\n", utils.Substring(ranString,6,2)) // ''
	fmt.Printf("Substring: %v\n", utils.Substring(ranString,10,15)) // ''
	fmt.Printf("Substring: %v\n", utils.Substring(ranString,-1,5)) // ''
	fmt.Printf("Substring: %v\n", utils.Substring(ranString,5,10)) // '9c919'

	arr := []int{ 1,2,3,4,5 }
	fmt.Printf("Array Before: %v\n", arr)
	utils.Slice.RemoveAt(&arr, 3) // Remove slice at index
	fmt.Printf("Array After 1: %v\n", arr)
	utils.Slice.AddTo(&arr, 4, 3)  // Add data to index 3
	fmt.Printf("Array After 2: %v\n", arr)
	utils.Slice.AddTo(&arr, 25, 1) // Add data to index 1
	fmt.Printf("Array After 3: %v\n", arr)
	utils.Slice.AddTo(&arr, 100) // Add data without specify index, will be added to last index
	fmt.Printf("Array After 4: %v\n", arr)
	utils.Slice.AddTo(&arr, -5) // Add data with minus index, will be added to first index which is treated as 0
	fmt.Printf("Array After 5: %v\n", arr)
	i := utils.Slice.Exist(&arr, 9)
	fmt.Println("Exist",i)
	j := utils.Slice.Exist(&arr, 100)
	fmt.Println("Exist",j)

	utils.FileMove("/root/project/filename","/root/project/newname", os.ModePerm) // Move file from path to path
	utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", os.ModePerm, "filename") // Move file from to new location with same name
	utils.FileMove("/root/project/oldfolder/","/root/project/newfolder/", os.ModePerm, "filename", "newname") // Move file from to new location with new name

	arrDuplicate := []int{1,0,0,2,3,2,4,5,6,7,4,4,7,4,7,7,7,15}
	fmt.Println(arrDuplicate)
	utils.Slice.Uniquify(&arrDuplicate)
	fmt.Println(arrDuplicate)

	objArr := []map[string]int{
		{"id": 4, "num": 2}, {"id": 3, "num": 9}, {"id": 0, "num": 1}, {"id": 8, "num": 2},
	}
	fmt.Printf("Where: %v\n", utils.Slice.Where(objArr, func(i int) bool {
		return objArr[i]["num"] == 2
	})) // Get all value in slice with condition

	fmt.Printf("First: %v\n", utils.Slice.First(objArr)) // Get first value in slice
	fmt.Printf("IndexOf: %v\n", utils.Slice.IndexOf(arrDuplicate, 15)) // Find first index of value in slice
	fmt.Printf("FirstWhere: %v\n", utils.Slice.FirstWhere(objArr, func(i int) bool {
		return objArr[i]["num"] == 9
	})) // Get first value in slice with given condition
	fmt.Printf("IndexWhere: %v\n", utils.Slice.IndexWhere(objArr, func(i int) bool {
		return objArr[i]["id"] == 0
	})) // Get first index in slice with given condition

	fmt.Printf("Last: %v\n", utils.Slice.Last(objArr)) // Get first value in slice
	fmt.Printf("LastIndexOf: %v\n", utils.Slice.LastIndexOf(arrDuplicate, 0)) // Find last index of value in slice
	fmt.Printf("LastWhere: %v\n", utils.Slice.LastWhere(objArr, func(i int) bool {
		return objArr[i]["id"] == 4
	})) // Get last value in slice with given condition
	fmt.Printf("LastIndexWhere: %v\n", utils.Slice.LastIndexWhere(objArr, func(i int) bool {
		return objArr[i]["num"] == 2
	})) // Get last index in slice with given condition
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddTo added in v1.0.2

func AddTo[T any](collections *[]T, collection T, index int)

func ConvertErrMsg added in v1.0.2

func ConvertErrMsg[R any](errF any) ([]*R, error)

func Distinct added in v1.0.2

func Distinct[T any](collections *[]T)

Distinct (collections): Distinct slices value

func Exist added in v1.0.2

func Exist[T any](collections []T, fn func(T) bool) (state bool)

func FileMove

func FileMove(src string, dst string, perm os.FileMode, file ...string)

func First added in v1.0.2

func First[T any](collections []T) (col T)

func FirstWhere added in v1.0.2

func FirstWhere[T any](collections []T, fn func(T) bool) (col T)

func GenerateSignature added in v1.0.2

func GenerateSignature(key string, second ...int64) (signature string)

func GetTimebaseUnix added in v1.0.2

func GetTimebaseUnix(seconds int64) int64

func IndexWhere added in v1.0.2

func IndexWhere[T any](collections []T, fn func(T) bool) (index int)

func IndexesOf added in v1.0.2

func IndexesOf[T any](collections []T, val T) (indexes []int)

func IndexesWhere added in v1.0.2

func IndexesWhere[T any](collections []T, fn func(T) bool) (indexes []int)

func IsRFC3339 added in v1.0.2

func IsRFC3339(field validator.FieldLevel) bool

func Last added in v1.0.2

func Last[T any](collections []T) (col T)

func LastIndexWhere added in v1.0.2

func LastIndexWhere[T any](collections []T, fn func(T) bool) (index int)

func LastWhere added in v1.0.2

func LastWhere[T any](collections []T, fn func(T) bool) (col T)

func Prepend added in v1.0.2

func Prepend[T any](collections []T, cols ...T) (res []T)

func RandString

func RandString(length int) string

func RandStringLower

func RandStringLower(length int) string

func RandStringUpper

func RandStringUpper(length int) string

func RandStringWithCharset

func RandStringWithCharset(length int, charset string) string

func RandomHex

func RandomHex(n int) string

func RandomHexUpper

func RandomHexUpper(n int) string

func RemoveAt added in v1.0.2

func RemoveAt[T any](collections *[]T, index int)

func RemoveIn added in v1.0.2

func RemoveIn[T any](collections *[]T, indexes []int)

func RemoveWhere added in v1.0.2

func RemoveWhere[T any](collections *[]T, fn func(T) bool)

func Substring

func Substring(text string, indexs ...int) string

func VerifySignature added in v1.0.2

func VerifySignature(key string, signature string, second ...int64) bool

func Where added in v1.0.2

func Where[T any](collections *[]T, fn func(T) bool)

Types

type CustomValidator added in v1.0.2

type CustomValidator struct {
	Validate   *validator.Validate
	Translator ut.Translator
}

func NewValidator added in v1.0.2

func NewValidator() *CustomValidator

func (*CustomValidator) GetErrMsg added in v1.0.2

func (cv *CustomValidator) GetErrMsg(err error) string

func (*CustomValidator) GetErrMsgField added in v1.0.2

func (cv *CustomValidator) GetErrMsgField(err error) (errF []ErrField)

func (*CustomValidator) SetTranslation added in v1.0.2

func (cv *CustomValidator) SetTranslation(locale string) *CustomValidator

type ErrField added in v1.0.2

type ErrField struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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