common

package module
v0.0.0-...-ce51046 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2022 License: MIT Imports: 38 Imported by: 0

README

common2

介绍

公共库文件

软件架构

软件架构说明

安装教程
  1. xxxx
  2. xxxx
  3. xxxx
使用说明
  1. xxxx
  2. xxxx
  3. xxxx
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
特技
  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/

Documentation

Index

Constants

View Source
const ContentType_Form = "application/x-www-form-urlencoded; charset=UTF-8"

常用类型

View Source
const ContentType_Html = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
View Source
const ContentType_Json = "application/json, text/plain, */*"
View Source
const ContentType_Xml = "application/xml, text/xml"

Variables

This section is empty.

Functions

func AddDate

func AddDate(startday string, addNum int) string

功能:增加日期 参数:startday例如"2018-12-01",addNum是增加的天数 返回:字符串形式的日期

func AesCbcDec

func AesCbcDec(encSrc string, key string, iv string) string

接口功能:AES解密(CBC模式,PKCS5padding填充) 参数: 返回:

func AesCbcEnc

func AesCbcEnc(src string, key string, iv string) string

接口功能:AES加密(CBC模式,PKCS5padding填充) 参数: 返回:

func AesEbcDec

func AesEbcDec(encSrc, key, iv string) string

接口功能:AES解密(EBC模式,nopadding填充) 参数: 返回:

func AesEbcEnc

func AesEbcEnc(src, key, iv string) string

接口功能:AES加密(EBC模式,nopadding填充) 参数: 返回:

func AppendToFile

func AppendToFile(fileName string, content string) error

功能:追加写文件 参数: fileName:文件名字(带全路径) content: 写入的内容 返回:

func Base64Dec

func Base64Dec(src string) []byte

功能:base64解码 参数: 返回:

func Base64Enc

func Base64Enc(src []byte) string

功能:base64编码 参数: 返回:

func Bool2String

func Bool2String(b bool) string

FormatBool returns "true" or "false" according to the value of b

func ByteToString

func ByteToString(src []byte) string

功能:byte转字符串 参数: 返回:

func BytesCombine

func BytesCombine(pBytes ...[]byte) []byte

功能:多个[]byte数组合并成一个[]byte 参数: 返回:

func CancelAutoRun

func CancelAutoRun(cmd string)

功能:取消自启动 参数: 返回:

func ConvertToString

func ConvertToString(src string, srcCode string, tagCode string) string

功能:字符串字符集转换 参数:src 来源字符串,srcCode当前字符集,tagCode目标字符集 返回: 例子:strResp = ConvertToString(strContents, "gbk", "utf8")

func ConvertjsonStringToNumber

func ConvertjsonStringToNumber(jsonstr string, key string) string

功能:将json里面的字符串变为数字 参数: 返回: 说明:有时候不规范的json里面同一个字段可能有不同类型出现,需要转为统一格式再处理 比如需要将"totalChargeAmount":"61.00" 转为 "totalChargeAmount":61.00

func CopyFile

func CopyFile(sourceFile, destinationFile string) error

功能:拷贝文件 参数:

返回:

func Cut

func Cut(src, prefix, suffix string) (string, error)

功能:剪切字符串 参数:prefix 前缀,suffix后缀 返回:前后缀中间的内容

func DelHtmlElement

func DelHtmlElement(src, el string) string

功能:删除指定的html元素 参数: 返回: 例子:DelHtmlElement(`66<td class="text-center">123</td>`, "td")

func Dial

func Dial(addr string) (*smtp.Client, error)

return a smtp client

func DoZlibCompress

func DoZlibCompress(src []byte) []byte

进行zlib压缩

func DoZlibUnCompress

func DoZlibUnCompress(compressSrc []byte) []byte

进行zlib解压缩

func DownloadFile

func DownloadFile(downloadUrl, localPath string) error

功能:下载文件 参数: 返回:

func FetchHtmlTbody

func FetchHtmlTbody(html string) (string, error)

功能:提取tbody 参数: 返回: 例子:

func Float64ToString

func Float64ToString(input_num float64) string

功能:float64 转 String工具类,保留6位小数 参数: 返回:

func FloatToString

func FloatToString(input_num float32) string

功能:float32 转 String工具类,保留6位小数 参数: 返回:

func FloatToStringN

func FloatToStringN(input_num float32, n int) string

功能:float32 转 String工具类,保留n位小数 参数: 返回:

func FormatStdDay

func FormatStdDay(str string) string

功能:格式化为标准日期 参数: 返回:字符串形式的日期,格式yyyy-mm-dd 例子:FormatStdDay("2019-1-5 23:00:01")

func FromUnicode

func FromUnicode(src string) string

功能:将unicode格式转为普通字符串 参数: 返回: 比如可以转换"\u80a5\u6a58\u9605\u8bfb-\u51cc\u83f2"

func Get302Cookie

func Get302Cookie(resp *http.Response) string

功能:获取302跳转之前的cookie 参数: 返回:

func GetClient

func GetClient(proxyAddr string) *http.Client

功能: 参数:proxyAddr 比如 "http://127.0.0.1:8888" 返回:

func GetConfigDirectory

func GetConfigDirectory() string

功能:获取当前配置文件的目录 参数: 返回:

func GetCookie

func GetCookie(resp *http.Response) string

功能:从返回头得到cookie 参数: 返回:

func GetFileList

func GetFileList(path string) []string

功能:搜索指定路径获取文件全路径列表(包含子目录) 参数: 返回:

func GetFileMd5

func GetFileMd5(filePath string) string

功能:获取文件的MD5数值 参数: 返回:

func GetFileModTime

func GetFileModTime(path string) int64

功能:获取文件修改时间 返回unix时间戳 参数: 返回:

func GetHour

func GetHour() int

功能:得到当前小时 参数: 返回:

func GetNow

func GetNow() string

功能:得到当前时间(北京时间) 参数: 返回:YYYY-MM-DD hh:mm:ss

func GetNumberPos

func GetNumberPos(str string) int

功能:获取数字的偏移量 参数: 返回:

func GetProcessDir

func GetProcessDir() string

功能:获取当前执行进程的绝对路径目录 参数: 返回:

func GetRequestParam

func GetRequestParam(rd *RequestData, key string) string

功能:读取参数,支持www和json格式的原始数据 参数: 返回:

func GetStringFromjson

func GetStringFromjson(jsonstr string, key string) string

功能:直接从json字符串提取键值 参数: 返回:

func GetToday

func GetToday() string

功能:得到当前时间(北京时间) 参数: 返回:YYYY-MM-DD 还有一种方式是 string(time.Now().Format("20060102150405")) 注意这个时间"2006/1/2 15:04:05" 是约定的数字表示年月日时分秒,用其它时间都不行

func GetUUID

func GetUUID() (string, error)

功能:生成guid 参数:

返回:

func GetUriFromHttpText

func GetUriFromHttpText(rawHttpText, methedName string) string

功能:从原始http报文提取uri 参数: 返回:

func GetUrlArg

func GetUrlArg(r *http.Request, name string) string

获取URL的GET参数 功能跟GetWWWParam一样的

func GetUserDir

func GetUserDir() string

功能:返回用户主目录 参数:

返回:用户主目录

func GetUserName

func GetUserName() string

功能:返回当前用户名字 参数:

返回:用户名字

func GetVariableParam

func GetVariableParam(arg ...string) []string

功能:可变参数转为字符串数组 参数: 返回:

func GetWWWParam

func GetWWWParam(r *http.Request, key string) string

功能:提取application/x-www-form-urlencoded参数,注意事先调用r.ParseForm() 参数: 返回:

func GetYesterday

func GetYesterday() string

func GetfieldFromHttpText

func GetfieldFromHttpText(rawHttpText, fieldName string) string

功能:从原始http报文提取对应的域 参数: 返回:

func HttpGet

func HttpGet(url, cookie string) (string, error)

功能:简单调用GET请求 参数: 返回:

func HttpMethod

func HttpMethod(method, url, body, contentType, cookie, referer string) (string, *http.Response, error)

功能:调用HTTP请求 参数:method可以是GET或POST,url网址,body是附带包体,accept接受的内容类型,cookie是可选的 返回:json数据

func HttpPost

func HttpPost(url, body, cookie string) (string, error)

功能:简单调用POST请求 参数: 返回:

func Int64ToString

func Int64ToString(src int64) string

功能:int64转字符串 参数: 返回:

func IntToString

func IntToString(src int) string

功能:int转字符串 参数: 返回:

func KillProcess

func KillProcess(text string) (string, error)

功能:杀进程 参数:

text 表示映像全程,比如notepad.exe

返回:

func ListAllProcess

func ListAllProcess() (string, error)

功能:列出所有进程 参数:

返回:

func ListProcess

func ListProcess(text string) (string, error)

功能:列出指定进程 参数:

text 表示映像全程,比如notepad.exe

返回:

func ListSubDir

func ListSubDir(dirPth string) (files []string, err error)

功能:获取指定目录下的所有子文件,不进入下一级目录搜索 参数: 返回:

func MD5

func MD5(str string) string

功能:对字符串进行MD5摘要 参数: 返回:

func Mkdir

func Mkdir(dirPath string) error

功能:创建目录 参数:

返回:

func ParseFileUploadRequest

func ParseFileUploadRequest(r *http.Request, key string) string

接口功能:解析处理文件上传的请求,并保存到磁盘 参数: 返回:保存在磁盘的全路径 key 是<input type="file" name="uploadfile">里面的uploadfile

func ParseHtmlTable

func ParseHtmlTable(body string) string

功能:解析表格,转换为字符串 参数: 返回: 例子:

func ParseRequest

func ParseRequest(r *http.Request, v interface{})

功能:解析HTTP请求 参数: 返回:

func ParseTimeString

func ParseTimeString(t string) time.Time

功能:将时间字符串转为time结构 参数: 返回:

func ParseUnitTime

func ParseUnitTime(sec int64) string

功能:解析unit时间 参数:sec是unit的时间,从1970年算起的,比如1559289405 返回:字符串形式的日期

func PathExists

func PathExists(path string) bool

功能:判断文件是否存在 参数: 返回:

func PathSeparator

func PathSeparator() string

功能:返回目录分隔符 参数:

返回:

func ReadConfig

func ReadConfig(node string, key string) string

功能:从当前配置文件读取键值 参数: 返回:

func ReadFile

func ReadFile(path string) []byte

功能:读文件到缓冲 参数: 返回:

func ReadHttpText

func ReadHttpText(r *http.Request) string

功能:读取http报文信息,如果业务需要调用r.ParseForm(),则它要放在r.ParseForm()之后执行 参数: 返回:

func ReadTextFile

func ReadTextFile(path string) string

功能:读文本文件到字符串 参数: 返回:

func RemoveDir

func RemoveDir(dirPath string) error

功能:删除目录 参数:

返回:

func RemoveDirAllItem

func RemoveDirAllItem(removeDir string)

功能:删除指定目录里面所有文件 参数:

返回:

func RemoveFile

func RemoveFile(filePath string) error

功能:删除文件 参数:

返回:

func RunProcess

func RunProcess(procPath string, arg ...string) int

功能:阻塞执行并返回进程退出码 参数:

返回:进程退出码 例子:common.RunProcess("msiexec", "/package", v.FilePath)

func RunProcess2

func RunProcess2(procPath string, arg ...string) (error, string)

功能:阻塞执行并返回2个信息 参数:

返回:错误信息、进程输出 例子:common.RunProcess2("ps","aux")

func RunProcess3

func RunProcess3(procPath string, arg ...string) (int, error, string)

功能:阻塞执行并返回3个信息 参数:

返回:进程退出码、错误信息、进程输出 还有一种实现方式这样,也可以,参考https://www.jianshu.com/p/1d3fc1003f39 cmd := exec.Command("tasklist") out, err := cmd.CombinedOutput()

if err != nil {
	log.Error(err)
}

func RunProcess3_windows_UAC

func RunProcess3_windows_UAC(procPath string, arg ...string) (int, error, string)

功能:windows平台下,使用管理员权限(UAC)阻塞执行并返回3个信息 参数:

返回:进程退出码、错误信息、进程输出 感谢:https://licoy.cn/3643.html

func RunProcess_windows_UAC

func RunProcess_windows_UAC(procPath string, arg ...string) int

功能:windows平台下,使用管理员权限(UAC)阻塞执行并返回进程退出码 参数:

返回:进程退出码 例子:common.RunProcess_windows_UAC("msiexec", "/package", v.FilePath)

func SendFileResponse

func SendFileResponse(w http.ResponseWriter, filePath string)

功能:发送HTTP回应,返回文件流 参数: 返回:

func SendMailUsingTLS

func SendMailUsingTLS(addr string, auth smtp.Auth, from string,
	to []string, msg []byte) (err error)

参考net/smtp的func SendMail() 使用net.Dial连接tls(ssl)端口时,smtp.NewClient()会卡住且不提示err len(to)>1时,to[1]开始提示是密送

func SendResponse

func SendResponse(w http.ResponseWriter, v interface{})

功能:发送HTTP回应 参数: 返回:

func SendToMail

func SendToMail(user, password, host, to, subject, body, mailtype string) error

功能:发邮件 参数: 返回: 例如:common.SendToMail("neo.chen.hz@foxmail.com", "123456", "smtp.qq.com:25", "neo.chen.hz@foxmail.com", "test", "test me", "text") 这个函数在golang 1.11有效,1.12开始golang的smtp库做了修改,禁止非tls方式传输,所以不生效了

func SendToMailBySSL

func SendToMailBySSL(user, password, host, port, to, subject, body, mailtype string)

功能:发加密邮件 参数: 返回:

func SetAutoRun

func SetAutoRun(cmd string)

功能:设置自启动 参数: 返回:

func SetDump

func SetDump()

func SetFileHidden

func SetFileHidden(filePath string, enable bool) error

功能:设置文件隐藏/可见 参数:

	filePath 路径
 enable 是否隐藏

返回:

func Sha256

func Sha256(src string) string

功能:计算sha256哈希值 参数: 返回:

func ShellExecute

func ShellExecute(filePath string) error

功能:调用shellExecute函数 参数:

filePath 路径

返回:

func SimpleLogInit

func SimpleLogInit()

功能:最简单进行日志初始化 参数: 返回:

func Sleep

func Sleep(second int)

功能:sleep 参数:秒 返回:无

func StartProcess

func StartProcess(procPath string, arg ...string) error

功能:非阻塞执行 参数:

返回:

func StartWith

func StartWith(src string, head string) bool

功能:判断字符串是否特定字符串开头 参数: 返回:

func String2Bool

func String2Bool(str string) bool

ParseBool returns the boolean value represented by the string. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.

func String2Float

func String2Float(str string) float32

功能:字符串转浮点 参数: 返回:

func String2Float64

func String2Float64(str string) float64

功能:字符串转浮点 参数: 返回:

func StringToByte

func StringToByte(src string) []byte

功能:字符串转byte 参数: 返回:

func StringToInt

func StringToInt(src string) int

功能:字符串转int 参数: 返回:

func StringToInt64

func StringToInt64(src string) int64

功能:字符串转int64 参数: 返回:

func SudoRunProcess

func SudoRunProcess(execCommand, pwd string) error

功能:linux使用特权身份阻塞执行(使用中间文件) 参数:

返回:

func SudoRunProcess2

func SudoRunProcess2(execCommand, pwd string) error

功能:linux使用特权身份阻塞执行(不使用中间文件) 这个函数还有bug,执行不成功 参数:

返回:

func TimeStringDiff

func TimeStringDiff(t1, t2 string) int64

功能:两个时间的差值 参数:t1和t2都是字符串形式的时间 返回:差值xx秒

func ToUnicode

func ToUnicode(src string) string

功能:转为unicode格式,比如"\u80a5\u6a58\u9605\u8bfb-\u51cc\u83f2" 参数: 返回:

func Unzip

func Unzip(zipFile string, destDir string) error

func UrlDec

func UrlDec(src string) string

功能:urlenc解码 参数: 返回:

func UrlEnc

func UrlEnc(src string) string

功能:urlenc编码 参数: 返回:

func WriteToFile

func WriteToFile(fileName string, content []byte, append bool) error

功能:写文件 参数: fileName:文件名字(带全路径) content: 写入的内容 append: 是否追加到末尾 返回:

func WriteToFile666

func WriteToFile666(fileName string, content []byte, append bool) error

功能:写文件,权限0666 参数: fileName:文件名字(带全路径) content: 写入的内容 append: 是否追加到末尾 返回:

func WriteToFile777

func WriteToFile777(fileName string, content []byte, append bool) error

功能:写文件,权限0777 参数: fileName:文件名字(带全路径) content: 写入的内容 append: 是否追加到末尾 返回:

func WriteToFileWithPerm

func WriteToFileWithPerm(fileName string, content []byte, append bool, perm fs.FileMode) error

功能:写文件 参数: fileName:文件名字(带全路径) content: 写入的内容 append: 是否追加到末尾 perm: 权限,常用的有0644、0666、0777 返回:

func XorDec

func XorDec(base64Slice string, key byte) string

功能:对字符串进行异或解密,支持中文 参数: 返回:

func XorEnc

func XorEnc(src string, key byte) string

功能:对字符串进行异或加密,支持中文 参数: 返回:

func Zip

func Zip(srcFile string, destZip string) error

srcFile could be a single file or a directory

Types

type Config

type Config struct {
	Mymap map[string]string
	// contains filtered or unexported fields
}

func (*Config) InitConfig

func (c *Config) InitConfig(path string)

func (*Config) InitConfig2

func (c *Config) InitConfig2(path string, haveStrcet bool)

func (Config) Read

func (c Config) Read(node, key string) string

type Flock

type Flock struct {
	LockFile string

	IsSuccess bool
	// contains filtered or unexported fields
}

func CreateFileLock

func CreateFileLock(file string) (f *Flock, e error)

创建文件锁,配合 defer f.Release() 来使用

func (*Flock) Lock

func (f *Flock) Lock() (e error)

上锁,配合 defer f.Unlock() 来使用

func (*Flock) Release

func (f *Flock) Release()

释放文件锁

func (*Flock) Unlock

func (f *Flock) Unlock()

解锁

type RequestData

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

func ParseHttpRequest

func ParseHttpRequest(r *http.Request) (*RequestData, error)

功能:解析和读取http.Request 参数: 返回: 说明:

POST /jumpPay HTTP/1.1 Host: 127.0.0.1 Connection: Keep-Alive Content-Length: 55 Content-Type: application/json

{"url":"http://www.baidu.com","params":"456","head":""}

Directories

Path Synopsis
aes

Jump to

Keyboard shortcuts

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