tools

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: May 8, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Overview

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2016-2023 volibear<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2016-2024 volibear<volibearw@gmail.com>. All rights reserved.

Package tools Copyright 2023 marcello<volibearw@gmail.com>. All rights reserved.

Package tools

Index

Constants

View Source
const (
	// RandomLowercase lowercase
	RandomLowercase randomType = 2 << iota
	// RandomMajuscule majuscule
	RandomMajuscule
	// RandomDigital digital
	RandomDigital
	// RandomSymbol symbol
	RandomSymbol
	// RandomAll all
	RandomAll = RandomLowercase | RandomMajuscule | RandomDigital | RandomSymbol
)
View Source
const IsDarwin = false
View Source
const IsLinux = true
View Source
const IsWindows = false
View Source
const TimeFormatString = "2006-01-02 15:04:05"

TimeFormatString time layout

Variables

This section is empty.

Functions

func Close

func Close(f io.Closer)

Close is a Go function that takes an io.Closer as a parameter and closes it.

The parameter `f` is the io.Closer that needs to be closed. This function does not return anything.

func Copy added in v0.0.13

func Copy(dst string, src ...string) error

Copy copies the contents of the file at the given source path to the destination path.

Parameters: - dst: the destination path where the file will be copied to. - src: the source path of the file to be copied.

Returns: - error: an error if the file copy operation fails.

func CopyFile added in v0.0.13

func CopyFile(dst, src string) error

CopyFile copies the contents of the file at the given source path to the destination path.

Parameters: - dst: the destination path where the file will be copied to. - src: the source path of the file to be copied.

Returns: - error: an error if the file copy operation fails.

func DirHasPrefix added in v0.0.13

func DirHasPrefix(s, prefix string) bool

DirHasPrefix checks if the directory path 's' has a prefix 'prefix'.

It takes in three parameters: - s (string): The directory path to check. - prefix (string): The prefix to compare against the directory path. - pathSeparator (rune): The path separator used in the directory path.

It returns a boolean value indicating whether the directory path has the specified prefix.

func FormatTime

func FormatTime[T constraints.Float | constraints.Integer](sec T, nsec T) string

FormatTime format time use TimeFormatString layout

func GetCurrentDirectory

func GetCurrentDirectory() string

GetCurrentDirectory return current execute file directory

func IsNumber

func IsNumber(s string) bool

IsNumber checks if the given string is a number.

Parameters: - s: the string to be checked.

Returns: - bool: true if the string is a number, false otherwise.

func JSONMarshalByte

func JSONMarshalByte(v interface{}) []byte

JSONMarshalByte ignore error, return JSON.Marshal []byte()

func JSONMarshalString

func JSONMarshalString(v interface{}) string

JSONMarshalString ignore error,return JSON.Marshal string

func Md5

func Md5(s string) string

Md5 calculates the MD5 hash of the input string.

It takes a string parameter and returns the MD5 hash as a string.// Md5 calculates the MD5 hash of the input string.

It takes a string parameter and returns the MD5 hash as a string.

func Md5File

func Md5File(filePath string) (string, error)

Md5File calculates the MD5 hash of a file specified by the filePath parameter.

It takes a string filePath parameter and returns the MD5 hash as a string and an error.

func PathExists

func PathExists(path string) (bool, error)

PathExists check path

func RandomString

func RandomString(length uint, charType randomType) (s string)

RandomString random a string

func RandomStringWithComplex added in v0.0.16

func RandomStringWithComplex(length uint) string

func RandomStringWithDigital added in v0.0.16

func RandomStringWithDigital(length uint) string

func RandomStringWithGeneral added in v0.0.16

func RandomStringWithGeneral(length uint) string

func RandomStringWithLowercase added in v0.0.16

func RandomStringWithLowercase(length uint) string

func RandomStringWithMajuscule added in v0.0.16

func RandomStringWithMajuscule(length uint) string

func ReCreateDirectory added in v0.0.14

func ReCreateDirectory(dir string) error

ReCreateDirectory removes the directory at the given path and creates a new empty directory at the same path.

Parameters: - dir: the path of the directory to be removed and recreated.

Returns: - error: an error if any occurred during the removal or creation of the directory.

func RemoveLastSeparator added in v0.0.14

func RemoveLastSeparator(path string) string

RemoveLastSeparator removes the last separator from the given path string.

Parameters: - path: the path string from which the last separator needs to be removed.

Returns: - string: the modified path string with the last separator removed.

func RemoveSameFromStringSlice

func RemoveSameFromStringSlice(slice []string) []string

RemoveSameFromStringSlice remove []string same element Deprecated: use SliceTrimSame replace

func ReverseByte

func ReverseByte(s []byte) []byte

ReverseByte reverse []byte

func SliceRemove

func SliceRemove[T comparable](remove []T, s []T) []T

SliceRemove remove value

func SliceTrimSame

func SliceTrimSame[T comparable](s []T) []T

SliceTrimSame remove same value

func Unzip added in v0.0.13

func Unzip(ctx context.Context, filename string, saveAs string, handler func(file **zip.File)) error

Unzip unzip zip file

func UnzipGBKHandler added in v0.0.13

func UnzipGBKHandler(f **zip.File)

func XMLMarshalByte added in v0.0.6

func XMLMarshalByte(v interface{}) []byte

XMLMarshalByte ignore error, return xml.Marshal []byte()

func XMLMarshalString added in v0.0.6

func XMLMarshalString(v interface{}) string

XMLMarshalString ignore error,return xml.Marshal string

func Zip added in v0.0.13

func Zip(zipPath string, paths ...string) error

Zip compresses the specified files or dirs to zip archive. If a path is a dir don't need to specify the trailing path separator. For example calling Zip("archive.zip", "dir", "csv/baz.csv") will get archive.zip and the content of which is baz.csv dir ├── bar.txt └── foo.txt Note that if a file is a symbolic link it will be skipped.

Types

type DownloadFile added in v0.0.14

type DownloadFile struct {
	// contains filtered or unexported fields
}

func (*DownloadFile) DisableCreateDirectory added in v0.0.14

func (d *DownloadFile) DisableCreateDirectory()

func (*DownloadFile) DownloadFromUrl added in v0.0.14

func (d *DownloadFile) DownloadFromUrl(ctx context.Context, url string) (n int64, err error)

DownloadFromUrl download from url

func (*DownloadFile) GetFilename added in v0.0.14

func (d *DownloadFile) GetFilename() string

GetFilename get filename

func (*DownloadFile) SaveAsDirectory added in v0.0.14

func (d *DownloadFile) SaveAsDirectory(directory string)

SaveAsDirectory save as directory, only directory

func (*DownloadFile) SaveAsFilename added in v0.0.14

func (d *DownloadFile) SaveAsFilename(filename string)

SaveAsFilename save as filename, only filename

func (*DownloadFile) Timeout added in v0.0.14

func (d *DownloadFile) Timeout(timeout time.Duration)

Timeout set timeout

type FTP added in v0.0.14

type FTP struct {

	// 连接ftp信息
	FTPAddress  string
	FTPUsername string
	FTPPassword string
	FTPTimeout  time.Duration
	// contains filtered or unexported fields
}

FTP ftp client

func NewFTP added in v0.0.14

func NewFTP(address, username, password string, timeout time.Duration) *FTP

func (*FTP) ChangeDir added in v0.0.14

func (w *FTP) ChangeDir(ctx context.Context, path string) (err error)

ChangeDir changes the directory on the FTP server.

It takes a context.Context and a path string as parameters and returns an error.

func (*FTP) Close added in v0.0.14

func (w *FTP) Close() (err error)

Close closes the FTP connection.

It returns an error if there was a problem quitting the FTP connection.

func (*FTP) Connect added in v0.0.14

func (w *FTP) Connect(ctx context.Context) (err error)

Connect establishes a connection to an FTP server using the provided context.

It takes a context.Context as a parameter and returns an error.

func (*FTP) Download added in v0.0.14

func (w *FTP) Download(ctx context.Context, path, saveAsDirectory string, saveAsFilename ...string) (err error)

Download downloads a file from an FTP server and saves it to a specified directory.

Parameters:

  • ctx: The context.Context used for the download request.
  • path: The path of the file to download from the FTP server.
  • saveAsDirectory: The directory where the downloaded file will be saved.
  • saveAsFilename: (Optional) The name of the file to save as. If not provided, the original filename will be used.

Returns: - err: An error if the download or file operations fail.

func (*FTP) GetServerConn added in v0.0.14

func (w *FTP) GetServerConn() *ftp.ServerConn

GetServerConn get ftp server connection

type PaginationData

type PaginationData struct {
	Total        int64   // total record
	Page         int64   // current page
	PageSize     int64   // show size every page
	PageNums     int64   // show digital numbers on page
	FirstPage    int64   // first page
	LastPage     int64   // last page
	PreviousPage int64   // previous page
	NextPage     int64   // next page
	TotalPage    int64   // total page
	Pages        []int64 // pages
}

PaginationData pagination data

func Pagination

func Pagination(total, page, pageSize int64, pageNum int64) (pg *PaginationData)

Pagination calculate total record page current page pageSize show size every page pagesNum show digital numbers on page

type TSMap added in v0.0.7

type TSMap[K, T any] struct {
	// contains filtered or unexported fields
}

TSMap thread safe map

func NewTSMap added in v0.0.12

func NewTSMap[K, T any]() *TSMap[K, T]

func (*TSMap[K, T]) Len added in v0.0.7

func (m *TSMap[K, T]) Len() int64

func (*TSMap[K, T]) Load added in v0.0.7

func (m *TSMap[K, T]) Load(key K) (T, bool)

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*TSMap[K, T]) LoadAndDelete added in v0.0.7

func (m *TSMap[K, T]) LoadAndDelete(key K) (T, bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*TSMap[K, T]) LoadOrStore added in v0.0.7

func (m *TSMap[K, T]) LoadOrStore(key K, value T) (T, bool)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*TSMap[K, T]) Range added in v0.0.7

func (m *TSMap[K, T]) Range(f func(key K, value T) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by f), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even f itself may call any method on m.

Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (*TSMap[K, T]) Store added in v0.0.7

func (m *TSMap[K, T]) Store(key K, value T)

type TSSlice added in v0.0.12

type TSSlice[T any] struct {
	// contains filtered or unexported fields
}

TSSlice thread safe slice

func NewTSSlice added in v0.0.12

func NewTSSlice[T any](def T) *TSSlice[T]

NewTSSlice new thread safe slice object

func (*TSSlice[T]) Append added in v0.0.12

func (s *TSSlice[T]) Append(v ...T)

Append append

func (*TSSlice[T]) Clone added in v0.0.12

func (s *TSSlice[T]) Clone() *TSSlice[T]

func (*TSSlice[T]) Get added in v0.0.12

func (s *TSSlice[T]) Get() []T

Get slice

func (*TSSlice[T]) Index added in v0.0.12

func (s *TSSlice[T]) Index(index int) (T, error)

Index get value

func (*TSSlice[T]) Insert added in v0.0.12

func (s *TSSlice[T]) Insert(i int, v ...T)

Insert element

func (*TSSlice[T]) Len added in v0.0.12

func (s *TSSlice[T]) Len() int

Len get slice length

func (*TSSlice[T]) Remove added in v0.0.12

func (s *TSSlice[T]) Remove(i, j int) error

Remove remove element by index

func (*TSSlice[T]) Reverse added in v0.0.12

func (s *TSSlice[T]) Reverse()

Reverse reverses the elements of the slice in place.

Jump to

Keyboard shortcuts

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