util

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package util provides common utility functions such as MD5, random numbers, path handling, etc.

Index

Constants

View Source
const (
	// USE_KEYIN is the initial value for enabling Keyin in Spider.
	USE_KEYIN = "\r\t\n"
)

Variables

This section is empty.

Functions

func Atoa

func Atoa(str interface{}) option.Option[string]

Atoa extracts a string from an interface{} value, returning None if nil.

func Atoi

func Atoi(str interface{}) option.Option[int]

Atoi extracts an int from an interface{} value, returning None if nil.

func Atoui

func Atoui(str interface{}) option.Option[uint]

Atoui extracts a uint from an interface{} value, returning None if nil.

func Bytes2String

func Bytes2String(b []byte) string

Bytes2String converts []byte to string via direct pointer conversion. Both share the same underlying memory; modifying one affects the other. Much faster than string([]byte{}) for large conversions.

func CheckErr

func CheckErr(err error)

CheckErr checks and logs the error if non-nil.

func CheckErrPanic

func CheckErrPanic(err error)

func ExcelSheetNameReplace

func ExcelSheetNameReplace(fileName string) string

ExcelSheetNameReplace replaces invalid Excel sheet name characters with underscores.

func FileNameReplace

func FileNameReplace(fileName string) string

FileNameReplace replaces invalid filename characters with similar alternatives.

func GetWDPath

func GetWDPath() string

The GetWDPath gets the work directory path.

func HashString

func HashString(encode string) uint64

func IsDirExists

func IsDirExists(path string) bool

The IsDirExists judges path is directory or not.

func IsFileExists

func IsFileExists(path string) bool

The IsFileExists judges path is file or not.

func IsNum

func IsNum(a string) bool

The IsNum judges string is number or not.

func JSONPToJSON added in v1.4.0

func JSONPToJSON(json string) string

JSONPToJSON modify jsonp string to json string Example: forbar({a:"1",b:2}) to {"a":"1","b":2}

func JSONString added in v1.4.0

func JSONString(obj interface{}) string

JSONString converts obj to a JSON string.

func KeyinsParse

func KeyinsParse(keyins string) []string

KeyinsParse splits user-provided custom keyins into unique tokens.

func MakeHash

func MakeHash(s string) string

MakeHash converts a string to a CRC32 hash hex string.

func MakeMd5

func MakeMd5(obj interface{}, length int) string

MakeMd5 creates an MD5 fingerprint for obj (method 2).

func MakeUnique

func MakeUnique(obj interface{}) string

MakeUnique creates a unique fingerprint for obj (method 1: FNV-64).

func Mkdir

func Mkdir(filePath string) result.VoidResult

Mkdir creates the directory for the given path.

func RandomCreateBytes

func RandomCreateBytes(n int, alphabets ...byte) []byte

RandomCreateBytes generate random []byte by specify chars.

func RelPath

func RelPath(targpath string) (ret result.Result[string])

RelPath converts targpath to a path relative to the current working directory.

func String2Bytes

func String2Bytes(s string) []byte

String2Bytes converts string to []byte via direct pointer conversion. Both share the same underlying memory; modifying one affects the other. Do not mutate the returned slice directly (e.g. b[1]='d') or the program may panic.

func WalkDir

func WalkDir(targpath string, suffixes ...string) (ret result.Result[[]string])

WalkDir walks directories under targpath, optionally filtered by suffixes.

func WalkFiles

func WalkFiles(targpath string, suffixes ...string) (ret result.Result[[]string])

WalkFiles walks files under targpath, optionally filtered by suffixes.

func WalkRelDir

func WalkRelDir(targpath string, suffixes ...string) (ret result.Result[[]string])

WalkRelDir walks directories under targpath and returns relative paths, optionally filtered by suffixes.

func WalkRelFiles

func WalkRelFiles(targpath string, suffixes ...string) (ret result.Result[[]string])

WalkRelFiles walks files under targpath and returns relative paths, optionally filtered by suffixes.

func XML2MapStr added in v1.4.0

func XML2MapStr(xmldoc string) map[string]string

XML2MapStr converts simple XML to a string map (supports UTF-8).

Types

This section is empty.

Jump to

Keyboard shortcuts

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