miniutils

package module
v1.0.11 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: MIT Imports: 28 Imported by: 15

README

🧰
Github | Gitee
入门级,简单,易用的Go小工具
助你成功转职Golang工程师!

简介

GoDoc License

Go实用小工具: 封装了高效开发的常用工具、函数集,简单易用。

快速开始

# 创建本地项目 myproject
go mod init myproject
# 新建入口文件 main.go
vim main.go

入口文件 main.go

package main

import (
	"fmt"
	"github.com/iotames/miniutils"
)

func main() {
    strfind := miniutils.NewStrfind("https://d.168.com/offer/356789.html")
	dofind := strfind.SetRegexp(`offer/(\d+)\.html`).DoFind()
	fmt.Println(dofind.GetOne(false)) // "356789"
	fmt.Println(dofind.GetOne(true)) // "offer/356789.html"
}
# 更新依赖
go mod tidy
# 运行
go run .

示例

测试样例

JWT 工具

JWT: 全称JSON Web Token,互联网API通讯接口身份验证的行业标准。通过JWT字符串的解密和验签,进行用户身份认证。参见: https://jwt.io/

package main

import (
	"fmt"
	"log"
	"time"
	"github.com/iotames/miniutils"
)

func main() {
	secret := miniutils.GetRandString(32) // 设置JWT签名密钥
	jwt := miniutils.NewJwt(secret) // 初始化JWT小工具
	jwtInfo := map[string]interface{}{"id": 1519512704946016256, "name": "Harvey", "age": 16, "mobile": "15988888888"}
	// 设置原始数据jwtInfo,有效期3600秒,创建JWT字符串tokenStr
	tokenStr, err := jwt.Create(jwtInfo, time.Second*3600)
	if err != nil {
		fmt.Printf("jwt.Create error: %v", err)
        return
	}
	log.Println("create JWT:", tokenStr)
	// 解码 JWT 字符串. 返回 map[string]interface{} 格式的数据。
	info, err := miniutils.NewJwt("").Decode(tokenStr)
	if err != nil {
		fmt.Printf("jwt.Decode error: %v", err)
        return
	}
	log.Println("jwt Decode:", info)
	// 解码 JWT 字符串并验签,验证有效期。 返回 map[string]interface{} 格式的数据。
	claims, err := jwt.Parse(tokenStr)
	if err != nil {
		fmt.Printf("jwt.Parse error: %v", err)
        return
	}
	log.Println("jwt Parse:", claims)
    
    _, err = jwt.Parse(tokenStr + "sign error")
	if err == miniutils.ErrTokenSign {
		fmt.Printf("JWT 签名错误")
	}
}

日志记录

	logger := miniutils.GetLogger("")
	logger.Debug("first log 11111")
	logger.Info("second log 22222")
	logger = miniutils.NewLogger("runtime/mylogs")
	logger.Debug("my logs 2333")
	logger.CloseLogFile()

字符串提取工具

    strfind := miniutils.NewStrfind("https://d.168.com/offer/356789.html")
	dofind := strfind.SetRegexp(`offer/(\d+)\.html`).DoFind()
	fmt.Println(dofind.GetOne(false)) // "356789"
	fmt.Println(dofind.GetOne(true)) // "offer/356789.html"

HTTP请求工具

	// 构建HTTP请求(默认GET方法)
    req := miniutils.NewHttpRequest("https://httpbin.org/get")
	// 设置HTTP请求头
	req.SetRequestHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.81 Safari/533.33")
	// 执行HTTP请求
	err := req.Do(nil)
	if err != nil {
		log.Println(err)
	}
	// 打印响应内容消息体
	log.Println(string(req.BodyBytes))

	req = miniutils.NewHttpRequest("https://httpbin.org/post")
	// 构建POST请求
	req.SetRequestPostByString("hello=word&some=2333")
	// 执行HTTP请求
	req.SetRequestHeader("xkey", "secretttkeyyy")
	err = req.Do(nil)
	if err != nil {
		log.Printf("request post do err (%v) \n", err)
	}
	// 打印HTTP响应对象
	log.Println(*req.Response)

	// 下载图片到本地
	miniutils.NewHttpRequest("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").Download("runtime/baidu.png")

Documentation

Index

Constants

View Source
const (
	LOG_LEVEL_DEBUG = 0
	LOG_LEVEL_INFO  = 1
	LOG_LEVEL_WARN  = 2
	LOG_LEVEL_ERROR = 3
)

Variables

View Source
var (
	ErrTokenFormat  = errors.New("token is not a JWT")
	ErrTokenExpired = errors.New("token is expired")
	ErrTokenSign    = errors.New("token sign error")
	ErrTokenExp     = errors.New("token lost field: exp")
)

Functions

func Base64UrlDecode

func Base64UrlDecode(seg string) ([]byte, error)

Base64UrlDecode Decode JWT specific base64url encoding with padding stripped

func Base64UrlEncode

func Base64UrlEncode(seg []byte) string

Base64UrlEncode Encode JWT specific base64url encoding with padding stripped

func CopyDir added in v1.0.2

func CopyDir(src string, dst string) error

CopyDir 复制整个目录到指定位置 copies a whole directory recursively

func CopyFile added in v1.0.2

func CopyFile(src, dst string) error

CopyFile 复制单个文件到指定位置(包含文件名)。copies a single file from src to dst

func GetBaseUrl added in v1.0.3

func GetBaseUrl(url string) string

GetBaseUrl. GetBaseUrl("https://www.baidu.com/hello?word=hiiii") -> "https://www.baidu.com" url must starwith http; return like: https://www.baidu.com

func GetDomainByUrl added in v1.0.3

func GetDomainByUrl(url string) string

GetDomainByUrl. 获取url网址的域名。 the arg url startwith http, //, / ; return like: "www.baidu.com", "baidu.com", ""

func GetFileInfo added in v1.0.8

func GetFileInfo(path string) (file fs.FileInfo, err error)

GetFileInfo 获取文件信息

func GetFileSha256

func GetFileSha256(path string) (hash string, err error)

GetFileSha256 get SHA256 hash of file.

func GetIndexOf added in v1.0.5

func GetIndexOf[T any](val T, vals []T) int64

GetIndexOf 获取切片元素的位置,不存在返回-1

func GetJwtBySecret

func GetJwtBySecret(keyBytes []byte, bodyInfo map[string]interface{}) (string, error)

func GetKeywordByDomain added in v1.0.3

func GetKeywordByDomain(domain string) string

GetKeywordByDomain GetKeywordByDomain("www.baidu.com") -> baidu

func GetMapStringValue added in v1.0.3

func GetMapStringValue(key string, dictMap map[string]interface{}) string

func GetPidByPort added in v1.0.2

func GetPidByPort(portNumber int) int

GetPidByPort. 查找端口所属进程的PID。 返回端口号对应的进程PID,若没有找到相关进程,返回-1

func GetPriceByText added in v1.0.5

func GetPriceByText(priceText string) float64

GetPriceByText 价格字符串转数字(float64) Examples: GetPriceByText("$ 156,335,10.37") -> 15633510.37 GetPriceByText("£156,335,10.37") -> 15633510.37

func GetRandInt added in v1.0.9

func GetRandInt(min, max int) int

GetRandInt 获取 min~max之间的随机数。包括min和max。

func GetRandString

func GetRandString(l int) string

func GetSha256

func GetSha256(s string) string

GetSha256 get SHA256 hash. The len of SHA256 value is 64.

func GetSha256BySecret

func GetSha256BySecret(secret string, keyBytes []byte) []byte

GetSha256BySecret get SHA256 hash by Key

func GetUrl added in v1.0.3

func GetUrl(url, base string) string

GetUrl. 获取Url链接全路径。常用于爬虫链接格式化。例: Get("/product?id=90", "https://www.site.com") url: startwith http, /, // ; base must startwith http

func GetUrlQueryValue added in v1.0.3

func GetUrlQueryValue(key, url string) string

GetUrlQueryValue 提取网址的查询字符串。即问号之后的部分。

func IsPathExists added in v1.0.2

func IsPathExists(path string) bool

IsPathExists 判断文件或文件夹是否存在

func JsonDecodeUseNumber

func JsonDecodeUseNumber(infoBytes []byte, result interface{}) error

JsonDecodeUseNumber 解析带数字的JSON

func KillPid added in v1.0.2

func KillPid(pid string) error

KillPid 杀死运行中的进程

func Md5

func Md5(s string) string

Md5 get the MD5 hash.

func Mkdir added in v1.0.2

func Mkdir(path string) error

Mkdir 创建目录

func ParseJsonFile added in v1.0.8

func ParseJsonFile(path string, v interface{}, useNumber bool) error

ParseJsonFile 解析json文件内容 json1 := map[string]interface{}{} ParseJsonFile("runtime/testjson.json", &json1, false)

func ReadCookies added in v1.0.8

func ReadCookies(h http.Header, filter string) []*http.Cookie

readCookies parses all "Cookie" values from the header h and returns the successfully parsed Cookies.

if filter isn't empty, only cookies of that name are returned

func ReadDir added in v1.0.2

func ReadDir(path string, callback func(fileinfo fs.FileInfo)) error

ReadDir 读取目录下的文件

func ReadFileToString added in v1.0.2

func ReadFileToString(path string) (string, error)

ReadFileString 读取文件内容

func ReadSetCookies added in v1.0.8

func ReadSetCookies(h http.Header) []*http.Cookie

readSetCookies parses all "Set-Cookie" values from the header h and returns the successfully parsed Cookies.

func ReplaceAllString added in v1.0.3

func ReplaceAllString(originalstr, oldstr, newstr string) string

func RunCmd added in v1.0.7

func RunCmd(name string, arg ...string) ([]byte, error)

RunCmd 直接执行操作系统中的命令 RunCmd("go", "version")

func StartBrowserByUrl added in v1.0.2

func StartBrowserByUrl(url string) error

StartBrowserByUrl 打开系统默认浏览器

func StrToInt added in v1.0.5

func StrToInt(intstr string, prune []string) int64

StrToInt 字符串转整数。prune参数: 去除千分位等特殊符号。 Examples: StrToInt("15,633,510", nil) -> 15633510 StrToInt("$ 156,335,10", []string{"$", ","}) != 15633510

func UpdateJsonFile added in v1.0.8

func UpdateJsonFile(path string, v interface{}) error

UpdateJsonFile 更新json文件内容 testJson := map[string]interface{}{"name": "Tom", "age": 19, "height": 167.5} UpdateJsonFile("runtime/testjson.json", testJson)

Types

type HttpRequest added in v1.0.4

type HttpRequest struct {
	Client    *http.Client
	Request   *http.Request
	OnRequest func(r *http.Request)
	Response  *http.Response
	BodyBytes []byte

	Url        string
	RetryTimes uint8
	// contains filtered or unexported fields
}

HttpRequest TODO 把 Request 改为私有属性

func NewHttpRequest added in v1.0.4

func NewHttpRequest(url string) *HttpRequest

NewHttpRequest 构建http请求,默认为GET方法的HTTP请求。

func (*HttpRequest) AddRequestHeader added in v1.0.11

func (h *HttpRequest) AddRequestHeader(key string, value string) error

SetRequestHeader 添加HTTP请求头

func (*HttpRequest) Do added in v1.0.4

func (h *HttpRequest) Do(callback func(h *HttpRequest)) error

Do 执行之构建好的HTTP请求

func (*HttpRequest) Download added in v1.0.4

func (h *HttpRequest) Download(file string) error

Download 下载文件. example: NewHttpRequest("https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png").Download("runtime/baidu.png")

func (*HttpRequest) ParseJsonResponse added in v1.0.4

func (h *HttpRequest) ParseJsonResponse(model interface{}) error

ParseJsonResponse 解码HTTP响应的json内容。 when response body is a json string param model: a struct point

func (*HttpRequest) SetProxy added in v1.0.4

func (h *HttpRequest) SetProxy(proxyUrl string)

设置HTTP代理。 例: SetProxy("http://127.0.0.1:1080")

func (*HttpRequest) SetRequestHeader added in v1.0.4

func (h *HttpRequest) SetRequestHeader(key string, value string) error

SetRequestHeader 设置HTTP请求头

func (*HttpRequest) SetRequestPost added in v1.0.4

func (h *HttpRequest) SetRequestPost(data interface{}) error

SetRequestPost 构建POST方法请求,设置POST数据内容。

func (*HttpRequest) SetRequestPostByString added in v1.0.4

func (h *HttpRequest) SetRequestPostByString(data string) error

SetRequestPostByString 构建POST方法请求,设置POST数据字符串。

func (*HttpRequest) SetTimeout added in v1.0.4

func (h *HttpRequest) SetTimeout(seconds uint8)

SetTimeout 设置请求超时时间

type JsonWebToken

type JsonWebToken struct {
	TokenString string
	// contains filtered or unexported fields
}

func NewJwt

func NewJwt(secret string) *JsonWebToken

NewJwt init JsonWebToken by secret string

func (*JsonWebToken) Create

func (j *JsonWebToken) Create(claims map[string]interface{}, expiresin time.Duration) (token string, err error)

Create JsonWebToken string Create(map[string]interface{}{"id": 123456789, "username": "Harvey"}, time.Second*time.Duration(3600))

func (*JsonWebToken) Decode

func (j *JsonWebToken) Decode(jwtStr string) (result map[string]interface{}, err error)

Decode 解码JWT字符串。reads the JsonWebToken string. Return the JWT decoded data.

func (*JsonWebToken) Parse

func (j *JsonWebToken) Parse(jwtStr string) (result map[string]interface{}, err error)

Parse 解码JWT字符串,并验证其有效性。reads the JsonWebToken string. Check the JWT decoded data and return.

type LogLevel added in v1.0.2

type LogLevel int

type Logger added in v1.0.2

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

func GetLogger added in v1.0.2

func GetLogger(dirpath string) *Logger

GetLogger 获取日志工具 logger:= GetLogger(""); logger.Debug("写一条日志111")

func NewLogger added in v1.0.2

func NewLogger(logsDir string) *Logger

TODO TO BE a global unique instance

func (*Logger) CloseLogFile added in v1.0.2

func (l *Logger) CloseLogFile()

func (*Logger) Debug added in v1.0.2

func (l *Logger) Debug(content ...interface{}) (isPrint bool)

func (*Logger) Debugf added in v1.0.9

func (l *Logger) Debugf(format string, v ...any) (isPrint bool)

func (*Logger) Error added in v1.0.2

func (l *Logger) Error(content ...interface{}) (isPrint bool)

func (*Logger) Errorf added in v1.0.9

func (l *Logger) Errorf(format string, v ...any) (isPrint bool)

func (*Logger) Info added in v1.0.2

func (l *Logger) Info(content ...interface{}) (isPrint bool)

func (*Logger) Infof added in v1.0.9

func (l *Logger) Infof(format string, v ...any) (isPrint bool)

func (*Logger) SetLogLevel added in v1.0.9

func (l *Logger) SetLogLevel(level LogLevel) *Logger

func (*Logger) Warn added in v1.0.2

func (l *Logger) Warn(content ...interface{}) (isPrint bool)

func (*Logger) Warnf added in v1.0.9

func (l *Logger) Warnf(format string, v ...any) (isPrint bool)

type Strfind added in v1.0.3

type Strfind struct {
	BodyStr string
	// contains filtered or unexported fields
}

func NewStrfind added in v1.0.3

func NewStrfind(bodyStr string) *Strfind

NewStrfind 字符串提取器。使用正则表达式从字符串中提取信息。

func (*Strfind) DoFind added in v1.0.3

func (s *Strfind) DoFind() *Strfind

DoFind 根据设定的正则表达式检索字符串

func (*Strfind) GetAll added in v1.0.3

func (s *Strfind) GetAll(matchFull bool) []string

GetAll 匹配所有检索结果

func (*Strfind) GetOne added in v1.0.3

func (s *Strfind) GetOne(matchFull bool) string

GetOne 获取检索结果。例: GetOne(true) -> "begin123end", GetOne(false) -> "123"

func (*Strfind) SetRegexp added in v1.0.3

func (s *Strfind) SetRegexp(rege string) *Strfind

SetRegexp 设置一个正则表达式。例:匹配数字 SetRegexp(`(?s:(\d+))`) OR SetRegexp(`(\d+)`)

func (*Strfind) SetRegexpBeginEnd added in v1.0.3

func (s *Strfind) SetRegexpBeginEnd(begin string, end string) *Strfind

SetRegexpBeginEnd 设置起始和结束字符串,提取匹配的字符串内容。 例: SetRegexpBeginEnd(`<script type=\"application/ld\+json\">`, `</script>`)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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