tool2

package module
v0.0.0-...-8541d41 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: Apache-2.0 Imports: 30 Imported by: 0

README

tool2

golang 常用工具函数

Documentation

Index

Constants

View Source
const (
	UTF8    = Charset("UTF-8")
	GB18030 = Charset("GB18030")
)
View Source
const (
	POST = "POST"
	GET  = "GET"
)
View Source
const LayoutDate = "2006-01-02"
View Source
const LayoutTime = "2006-01-02 15:04:05"

Variables

This section is empty.

Functions

func NewPool

func NewPool() *workerPool

创建协程池

Types

type Browser

type Browser struct {
}

func NewBrowser

func NewBrowser() *Browser

func (*Browser) OpenUrl

func (*Browser) OpenUrl(url string) (err error)

打开网页

type Bytes

type Bytes struct {
}

func NewBytes

func NewBytes() *Bytes

func (*Bytes) ToStr

func (*Bytes) ToStr(b []byte) string

byte转字符串

func (*Bytes) ToStrByCharset

func (*Bytes) ToStrByCharset(byte []byte, charset Charset) (ret string)

byte转字符串指定字符集

type Charset

type Charset string

type Command

type Command struct {
}

func NewCmd

func NewCmd() *Command

func (*Command) Exec

func (obj *Command) Exec(commName string, arg ...string) (string, error)

执行命令并返回结果

func (*Command) ExecGrep

func (obj *Command) ExecGrep(commName string, arg ...string) (string, error)

func (*Command) ExecPipe

func (obj *Command) ExecPipe(commName string, arg ...string) error

执行命令并直接输出结果

func (*Command) FlagBool

func (*Command) FlagBool(name string, value bool, usage string) bool

解析命令行bool参数

func (*Command) FlagFloat64

func (*Command) FlagFloat64(name string, value int64, usage string) float64

解析命令行int64参数

func (*Command) FlagInt

func (*Command) FlagInt(name string, value int, usage string) int

解析命令行int参数

func (*Command) FlagInt64

func (*Command) FlagInt64(name string, value int64, usage string) int64

解析命令行int64参数

func (*Command) FlagString

func (*Command) FlagString(name, value, usage string) string

解析命令行字符串参数

type File

type File struct {
}

func NewFile

func NewFile() *File

func (*File) AppendWriteFile

func (*File) AppendWriteFile(filePath string, body []byte) (writerLen int, err error)

追加写入

func (*File) CreateFile

func (*File) CreateFile(filePath string, body []byte) (writerLen int64, err error)

覆盖写入文件

func (*File) CreateFileByNot

func (obj *File) CreateFileByNot(filePath string) error

如果文件夹不存在,则递归创建文件夹

func (*File) FileSize

func (*File) FileSize(file string) int64

获取文件大小

func (*File) IsBinary

func (*File) IsBinary(content string) bool

IsBinary determines whether the specified content is a binary file content.

func (*File) IsExist

func (*File) IsExist(path string) bool

判断所给路径文件/文件夹是否存在

func (*File) ReadAll

func (*File) ReadAll(fielPath string) ([]byte, error)

一次性读取文件所有内容

func (*File) ReadAllOnLine

func (*File) ReadAllOnLine(fielPath string, callBak func(row string) bool)

一次性加载文件,按行读取

func (*File) ReadOnLine

func (*File) ReadOnLine(fielPath string, callBak func(row string) bool)

逐行读取文件

func (File) WriteFileSafer

func (File) WriteFileSafer(writePath string, data []byte, perm os.FileMode) error

WriteFileSafer writes the data to a temp file and atomically move if everything else succeeds.

type Folder

type Folder struct {
}

func NewFolder

func NewFolder() *Folder

func (*Folder) IsDir

func (*Folder) IsDir(path string) bool

func (*Folder) ReadDirs

func (*Folder) ReadDirs(dir string) ([]string, error)

读取目录中的目录

func (*Folder) ReadFiles

func (*Folder) ReadFiles(dir string) (fileList []os.FileInfo, err error)

获取当前文件夹中的所有文件

func (*Folder) ReadFilesByOrderNum

func (*Folder) ReadFilesByOrderNum(dir string) (fileList []string, err error)

读取目录文件并按文件名中的数字排序

func (*Folder) ReadFilesToStr

func (*Folder) ReadFilesToStr(dir string) (fileList []string, err error)

获取当前文件夹中的所有文件

type Grab

type Grab struct {
}

func NewGrab

func NewGrab() *Grab

func (*Grab) Fetch

func (obj *Grab) Fetch(url string) (doc *goquery.Document, err error)

请求url获取文档对象

func (*Grab) FindList

func (obj *Grab) FindList(url, find string, callBack func(i int, list *goquery.Selection)) error

获取url查找内容列表

func (*Grab) FindOne

func (obj *Grab) FindOne(url, find string, callBack func(sel *goquery.Selection)) error

获取url查找第一个内容

type Http

type Http struct {
}

func NewHttp

func NewHttp() *Http

func (*Http) DownloadFile

func (obj *Http) DownloadFile(url, savePath string) (string, error)

* 下载文件 url:下载地址 savePath:保存路径(包含文件名)

func (*Http) Get

func (obj *Http) Get(url string, data map[string]interface{}, header map[string]string) ([]byte, error)

http get请求

func (*Http) GetUrlBody

func (*Http) GetUrlBody(url string) ([]byte, error)

* 获取url返回的内容

func (*Http) GetUrlRedirect

func (*Http) GetUrlRedirect(url string) (*url.URL, error)

* 获取Url地址重写向地址

func (*Http) Post

func (obj *Http) Post(url string, data map[string]interface{}, header map[string]string) ([]byte, error)

http post请求

func (*Http) Request

func (obj *Http) Request(method, requestUrl string, data map[string]interface{}, header map[string]string) ([]byte, error)

http 通用请求

type Json

type Json struct {
}

func NewJson

func NewJson() *Json

func (*Json) MarshalIndentJSON

func (*Json) MarshalIndentJSON(v interface{}, prefix, indent string) ([]byte, error)

func (*Json) MarshalJSON

func (*Json) MarshalJSON(v interface{}) ([]byte, error)

func (*Json) UnmarshalJSON

func (*Json) UnmarshalJSON(data []byte, v interface{}) error

type Map

type Map struct {
}

func NewMap

func NewMap() *Map

func (*Map) Int64BoolToKeys

func (*Map) Int64BoolToKeys(req map[int64]bool) (res []int64)

func (*Map) Int64Int64ToKeys

func (*Map) Int64Int64ToKeys(req map[int64]int64) (res []int64)

func (*Map) Int64Int64ToValues

func (*Map) Int64Int64ToValues(req map[int64]int64) (res []int64)

func (*Map) Int64StrToKeys

func (*Map) Int64StrToKeys(req map[int64]string) (res []int64)

func (*Map) Int64StrToValues

func (*Map) Int64StrToValues(req map[int64]string) (res []string)

func (*Map) IntBoolToKeys

func (*Map) IntBoolToKeys(req map[int]bool) (res []int)

func (*Map) IntIntToKeys

func (*Map) IntIntToKeys(req map[int]int) (res []int)

func (*Map) IntIntToValues

func (*Map) IntIntToValues(req map[int]int) (res []int)

func (*Map) IntStrToKeys

func (*Map) IntStrToKeys(req map[int]string) (res []int)

func (*Map) IntStrToValues

func (*Map) IntStrToValues(req map[int]string) (res []string)

func (*Map) StrBoolToKeys

func (*Map) StrBoolToKeys(req map[string]bool) (res []string)

func (*Map) StrInt64ToKeys

func (*Map) StrInt64ToKeys(req map[string]int64) (res []string)

func (*Map) StrInt64ToValues

func (*Map) StrInt64ToValues(req map[string]int64) (res []int64)

func (*Map) StrIntToKeys

func (*Map) StrIntToKeys(req map[string]int) (res []string)

func (*Map) StrIntToValues

func (*Map) StrIntToValues(req map[string]int) (res []int)

func (*Map) StrStrToKeys

func (*Map) StrStrToKeys(req map[string]string) (res []string)

func (*Map) StrStrToValues

func (*Map) StrStrToValues(req map[string]string) (res []string)

type Net

type Net struct {
}

func NewNet

func NewNet() *Net

func (*Net) LocalIP

func (*Net) LocalIP() (string, error)

func (*Net) LocalMac

func (*Net) LocalMac() (string, error)

func (*Net) TcpGather

func (*Net) TcpGather(ip, port string) bool

tcp端口检测

type Num

type Num struct {
}

func NewNum

func NewNum() *Num

func (*Num) FloatDecimal

func (*Num) FloatDecimal(num float64) float64

fload64保留两位小数

func (*Num) FloatToInt64

func (*Num) FloatToInt64(num float64, retain int) int64

* num: 数字 retain:保留位数,精度

func (*Num) FloatToStr

func (*Num) FloatToStr(f float64) string

float转字符串 'b' (-ddddp±ddd,二进制指数) 'e' (-d.dddde±dd,十进制指数) 'E' (-d.ddddE±dd,十进制指数) 'f' (-ddd.dddd,没有指数) 'g' ('e':大指数,'f':其它情况) 'G' ('E':大指数,'f':其它情况)

func (*Num) Int64ToDateStr

func (*Num) Int64ToDateStr(i int64) string

时间戳转日期字符串

func (*Num) Int64ToFloat64

func (*Num) Int64ToFloat64(num int64, retain int) float64

* num: 数字 retain:保留位数,精度

func (*Num) Int64ToStr

func (*Num) Int64ToStr(i int64) string

int64转字符串

func (*Num) Int64ToTimeStr

func (*Num) Int64ToTimeStr(i int64) string

时间戳转时间字符串

func (*Num) IntToStr

func (*Num) IntToStr(i int) string

int转字符串

func (*Num) MaxFloat

func (*Num) MaxFloat(a, b float64) float64

FLOAT最大值

func (*Num) MaxInt

func (*Num) MaxInt(a, b int) int

INT最大值

func (*Num) MaxInt64

func (*Num) MaxInt64(a, b int64) int64

INT64最大值

func (*Num) MinFloat

func (*Num) MinFloat(a, b float64) float64

FLOAT最小值

func (*Num) MinInt

func (*Num) MinInt(a, b int) int

INT最小值

func (*Num) MinInt64

func (*Num) MinInt64(a, b int64) int64

INT64最小值

type OS

type OS struct {
}

func NewOS

func NewOS() *OS

func (*OS) Home

func (obj *OS) Home() (string, error)

func (*OS) IsDarwin

func (*OS) IsDarwin() bool

IsDarwin

func (*OS) IsLinux

func (*OS) IsLinux() bool

IsLinux

func (*OS) IsWindows

func (*OS) IsWindows() bool

IsWindows

func (*OS) Pwd

func (*OS) Pwd() string

Pwd

type Rand

type Rand struct {
}

func NewRand

func NewRand() *Rand

func (*Rand) RandRange

func (*Rand) RandRange(min, max int) int

返回范围内随机数

func (*Rand) String

func (*Rand) String(length int) string

String returns a random string ['a', 'z'] and ['0', '9'] in the specified length.

type Slice

type Slice struct {
}

func NewSlice

func NewSlice() *Slice

func (*Slice) Int64ToMap

func (*Slice) Int64ToMap(req []int64) (res map[int64]bool)

func (*Slice) Int64ToSql

func (*Slice) Int64ToSql(req []int64) (query []string, args []int64)

func (*Slice) Int64ToStr

func (*Slice) Int64ToStr(req []int64) (res []string)

func (*Slice) IntToMap

func (*Slice) IntToMap(req []int) (res map[int]bool)

func (*Slice) IntToSql

func (*Slice) IntToSql(req []int) (query []string, args []int)

func (*Slice) IntToStr

func (*Slice) IntToStr(req []int) (res []string)

func (*Slice) StrToInt64

func (*Slice) StrToInt64(req []string) (res []int64)

func (*Slice) StrToMap

func (*Slice) StrToMap(req []string) (res map[string]bool)

func (*Slice) StrToSql

func (*Slice) StrToSql(req []string) (query, args []string)

type Str

type Str struct {
}

func NewStr

func NewStr() *Str

func (*Str) Contains

func (*Str) Contains(str string, strs []string) bool

Contains determines whether the str is in the strs.

func (*Str) EncloseIgnoreCase

func (*Str) EncloseIgnoreCase(text, open, close string, searchStrs ...string) string

Enclose encloses search strings with open and close, case-insensitively.

func (*Str) PYSort

func (*Str) PYSort(strArr []string) []string

中文拼音排序

func (*Str) ReplaceEmpty

func (*Str) ReplaceEmpty(str string, rep ...string) string

字符串替换

func (*Str) ReplaceIgnoreCase

func (*Str) ReplaceIgnoreCase(text, searchStr, repl string) string

ReplaceIgnoreCase replace searchStr with repl in the text, case-insensitively.

func (*Str) ReplaceRegexpStrEmpty

func (*Str) ReplaceRegexpStrEmpty(str string, math ...string) string

字符串正则替换

func (*Str) ReplacesIgnoreCase

func (*Str) ReplacesIgnoreCase(text string, searchStrRepl ...string) string

ReplacesIgnoreCase replace searchStr-repl pairs in the text, case-insensitively.

func (*Str) SplitByNum

func (*Str) SplitByNum(txt string, length int) []string

字符串拆分,按字数

func (*Str) SubStr

func (*Str) SubStr(str string, length int) string

高效截取字符串

func (*Str) ToBytes

func (*Str) ToBytes(s string) []byte

字符串转byte

func (*Str) ToDateStr

func (obj *Str) ToDateStr(t string) (string, error)

时间戳字符串转日期字符串

func (*Str) ToFloat64

func (*Str) ToFloat64(s string) (float64, error)

字符串转float

func (*Str) ToInt

func (*Str) ToInt(s string) (int, error)

字符串转int

func (*Str) ToInt64

func (*Str) ToInt64(s string) (int64, error)

字符串转int64

func (*Str) ToTime

func (*Str) ToTime(s string) (time.Time, error)

日期字符串转时间对象

func (*Str) ToTimeStr

func (obj *Str) ToTimeStr(s string) (string, error)

int字符串转时间字符串

type Time

type Time struct {
}

func NewTime

func NewTime() *Time

func (*Time) DateTimeToUnix

func (*Time) DateTimeToUnix(t string) (int64, error)

日期字符串转时间戳

func (*Time) GetDate

func (*Time) GetDate() string

返回年月日

func (*Time) GetTime

func (*Time) GetTime() string

返回年月日时分秒

func (*Time) MilliSecondToDateTime

func (*Time) MilliSecondToDateTime(t int64) string

毫秒转日期

func (*Time) RunTime

func (*Time) RunTime(callback func()) time.Duration

计算程序运行时间

func (*Time) SleepRange

func (*Time) SleepRange(max, min int)

随机睡眠指定范围毫秒

func (*Time) UnixToDateTime

func (*Time) UnixToDateTime(t int64) string

时间戳转日期

Jump to

Keyboard shortcuts

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