utils

package
v1.11.3 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: MIT Imports: 17 Imported by: 27

README

Utilities

Common utils

Usage

Quick usage

import "github.com/gflydev/core/utils"

newHello := utils.CopyStr("Hello world")
Bytes
// Random byte
myBytes := utils.RandByte(make([]byte, 15))

// Extend bytes
myBytes := utils.ExtendByte(myBytes, 5)
Token
// Random UNIQUE token
myToken := utils.Token()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComparePasswords added in v1.10.4

func ComparePasswords(hashedPwd, inputPwd string) bool

ComparePasswords func for a comparing password.

func CopyByte

func CopyByte(b []byte) []byte

CopyByte returns a copy of byte slice in a new pointer.

func CopyStr

func CopyStr(s string) string

CopyStr copies a string to make it immutable

func EqualByte

func EqualByte(a, b []byte) bool

EqualByte reports whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice.

func ExtendByte

func ExtendByte(b []byte, needLen int) []byte

ExtendByte extends b to needLen bytes.

Example b have [10 23 34 129], needLen 2. So return [10 23] Example b have [10 23 34 129], needLen 10. So return [10 23 34 129 0 0 0 0 0 0]

func FileExt

func FileExt(fileName string) string

FileExt Extract extension of file.

Eg Get `jpeg` from `https://902-local.s3.us-west-1.amazonaws.com/news/Avatar2023.jpeg`
	Or `63e85ba1.png` from `storage/tmp/63e85ba1.png`

func GeneratePassword added in v1.10.4

func GeneratePassword(p string) string

GeneratePassword func for a making hash & salt with user password.

func Getenv

func Getenv[V any](key string, init V) V

Getenv Get environment parameter (From .env file).

func IncludeStr

func IncludeStr(slice []string, s string) bool

IncludeStr returns true or false if given string is in slice.

func IndexOfStr

func IndexOfStr(slice []string, s string) int

IndexOfStr returns index position in slice from given string If value is -1, the string does not found.

func MD5

func MD5(s string) string

MD5 hash md5 from string value

func PrependByte

func PrependByte(dst []byte, src ...byte) []byte

PrependByte prepends bytes into a given byte slice.

Example dst have [10 23 34] src is 44 54. So return [44 54 10 23 34]

func PrependByteStr

func PrependByteStr(dst []byte, src string) []byte

PrependByteStr prepends a string into a given byte slice.

func QuoteStr

func QuoteStr(raw string) string

QuoteStr escape special characters in a given string

func RandByte

func RandByte(dst []byte) []byte

RandByte returns dst with a cryptographically secure string random bytes.

NOTE: Make sure that dst has the length you need.

func RandInt64

func RandInt64(m int64) int64

RandInt64 Generate a random number Int64 in rang [0, max)

NOTE:

Get error `G404 (CWE-338): Use of weak random number generator (math/rand instead of crypto/rand)
(Confidence: MEDIUM, Severity: HIGH)` when use `rand.Intn(max)` from "math/rand".
Fixed Refer https://github.com/securego/gosec/issues/294#issuecomment-487452731

func ReflectType

func ReflectType(obj interface{}) string

ReflectType Get the name of a struct instance.

func RenameFile

func RenameFile(fileName, newName string) string

RenameFile Extract new file path.

Eg Get `hello.jpeg` from `Avatar2023.jpeg` and `hello`
	Or `storage/tmp/hello.png` from `storage/tmp/63e85ba1.png` and `hello`

func RequestParam added in v1.10.1

func RequestParam(urlStr, param string) (string, error)

RequestParam parse a specific request parameter string.

Eg Get `G-Key=e2c32ed0807ce086083281f131&G-Time=20240803130551`
	from `http://localhost:7789/api/v1/uploads?G-Key=e2c32ed0807ce086083281f131&G-Time=20240803130551`

func RequestPath added in v1.10.1

func RequestPath(urlStr string) (string, error)

RequestPath parse request path string.

Eg Get `/news/Avatar2023-jcer.jpeg`
		from `https://902-local.s3.us-west-1.amazonaws.com/news/Avatar2023-jcer.jpeg`
	Or `/storage/tmp/63e85ba1.jpeg`
		from `https://www.dancefitvn.com/storage/tmp/63e85ba1.jpeg`

func RequestURL added in v1.10.4

func RequestURL(urlStr string) (string, error)

RequestURL parse request URL string

func Sha256

func Sha256(args ...any) string

Sha256 hash from list of arguments

func Token

func Token(object ...string) string

Token generate unique token

func UnpackArray added in v1.7.1

func UnpackArray(arr any) []any

UnpackArray Unpack an `arr` argument `any` but is exactly a type `[]any` Eg: We have `args` type `any`. var args any args = []string{"my-file.pdf", "png"} utils.UnpackArray(args)

func UnpackArrayT added in v1.7.1

func UnpackArrayT[T any](arr any) (r []any)

UnpackArrayT Unpack an `a` argument `any` but is exactly a type `[]T` Eg: We have `args` type `any`. var args any args = []string{"my-file.pdf", "png"} utils.UnpackArrayT[string](args)

func UnsafeBytes

func UnsafeBytes(s string) []byte

UnsafeBytes returns a byte pointer without allocation.

func UnsafeStr

func UnsafeStr(b []byte) string

UnsafeStr returns a string pointer without allocation.

Types

This section is empty.

Jump to

Keyboard shortcuts

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