tool

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: MIT Imports: 23 Imported by: 0

README

tool

golang common utils functions

install

go get -u github.com/fuxingZhang/tool

doc

see here

function lists

func Retry(attempts int, sleep time.Duration, fn func() error) error
func Download(url, filepath string) error
func DownloadWithOptions(url, filepath string, option DownloadFileOption) (result DownloadFileResult)
func GetFuncName(fn interface{}) string 
func GetFuncNameWithPath(fn interface{}) string 
func PrettyPrint(v interface{}) 
func TrimStruct(src interface{}) error 
func CopyStruct(src, dst interface{}) error
func GetFileMd5(path string) (md5Val string, err error) 
func CheckFileExists(filepath string) (exist bool, err error) 
func CheckDirExists(path string) (exist bool, err error) 
func CheckPathExists(path string) (exist bool, err error)
func RandInt64(min, max int64) (num int64, err error)
func RandomStr(n int) string 
func TailFileBySystemCommand(path string, n int) (data []string, err error)
func TailFile(path string, n int) (data []string, err error) 
func LookupHost(dns, host string) (addrs []string, err error) 
func CopyDir(src string, dest string) error 
func CopyFile(src string, dest string) error 

usage

package main

import (
    "errors"
    "fmt"

    "github.com/fuxingZhang/tool"
)

func main() {
    err := tool.Retry(3, time.Millisecond*100, func() error {
        return tool.Download("http://localhost:8080/public/test.sh", "./test.sh")
    })
    fmt.Println(err)
}

sub package

slice
function lists
func ContainsInt(ints []int, val int) bool 
func ContainsStr(strs []string, s string) bool 
usage
package main

import (
    "errors"
    "fmt"

    "github.com/fuxingZhang/tool/slice"
)

func main() {
    fmt.Println(slice.ContainsInt([]int{1, 2, 3}, 1))
    fmt.Println(slice.ContainsStr([]string{"a", "b"}, "c"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckDirExists added in v0.1.4

func CheckDirExists(path string) (exist bool, err error)

CheckDirExists check if a directory exists

func CheckFileExists

func CheckFileExists(path string) (exist bool, err error)

CheckFileExists check if a file exists

func CheckPathExists added in v0.1.4

func CheckPathExists(path string) (exist bool, err error)

CheckPathExists check if a path exists

func CopyDir added in v0.2.1

func CopyDir(src string, dest string) error

CopyDir copy folder from src to dest

func CopyFile added in v0.2.1

func CopyFile(src string, dest string) error

CopyFile copy file from src to dest

func CopyStruct

func CopyStruct(src, dst interface{}) error

CopyStruct Struct copy

func Download

func Download(url, filepath string) (err error)

Download download file

func EncodeURI added in v0.2.1

func EncodeURI(rawURL string) (string, error)

EncodeURI Encode URI

func GetFileMd5

func GetFileMd5(path string) (md5Val string, err error)

GetFileMd5 get file md5 string

func GetFilenameFromHeader added in v0.2.1

func GetFilenameFromHeader(header http.Header) (string, error)

GetFilenameFromHeader get filename from header

func GetFuncName

func GetFuncName(fn interface{}) string

GetFuncName get function name

func GetFuncNameWithPath

func GetFuncNameWithPath(fn interface{}) string

GetFuncNameWithPath get function name with filepath

func Go

func Go(cb func())

Go goroutine with recover

func LookupHost added in v0.2.0

func LookupHost(dns, host string) (addrs []string, err error)

LookupHost looks up the given host using the local resolver. It returns a slice of that host's addresses.

func PrettyPrint

func PrettyPrint(v interface{})

PrettyPrint use json.Indent print json

func RandInt64

func RandInt64(min, max int64) (num int64, err error)

RandInt64 Generate random int64 of specified length

func RandomStr

func RandomStr(n int) string

RandomStr Generate random string of specified length

func Retry

func Retry(attempts int, sleep time.Duration, fn func() error) (err error)

Retry retry function

func Reverse added in v0.1.9

func Reverse[V any](s []V)

Reverse reverse slice

func TailFile added in v0.1.9

func TailFile(path string, n int) (data []string, err error)

TailFile get last n line from a file

func TailFileBySystemCommand added in v0.1.9

func TailFileBySystemCommand(path string, n int) (data []string, err error)

TailFileBySystemCommand get last n line from a file use system command

func TrimStruct

func TrimStruct(src interface{}) error

TrimStruct Remove spaces from both sides of a string in a struct

Types

type DownloadFileOption added in v0.1.7

type DownloadFileOption struct {
	Headers      map[string]string
	Timeout      time.Duration
	IsReturnMD5  bool
	IsWithCancel bool
}

DownloadFileOption download file option

type DownloadFileResult added in v0.1.7

type DownloadFileResult struct {
	Err    error
	Md5    string
	Cancel context.CancelFunc
}

DownloadFileResult dowload file result

func DownloadWithOptions added in v0.1.7

func DownloadWithOptions(url, filepath string, option DownloadFileOption) (result DownloadFileResult)

DownloadWithOptions download file with headers and return md5 string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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