tools

package module
v0.2.52 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2023 License: Apache-2.0 Imports: 23 Imported by: 43

README

tools

go的工具集

安装

go get github.com/PeterYangs/tools

1.网络请求

网络请求已拆分到新仓库https://github.com/PeterYangs/request


import "github.com/PeterYangs/tools/http"

//获取客户端
client := http.Client()

//get请求
str, err := client.Request().GetToString("https://www.baidu.com")

//post请求
str, err := client.Request().PostToString("https://www.baidu.com")



//携带参数
p := map[string]interface{}{"name": []string{"123", "456"}, "age": 1, "nickname": "123"}

str, err := client.SetTimeout(1 * time.Second).Request().SetParameter(p).GetToString("https://www.google.com/")


//复杂参数
p := map[string]interface{}{
		"name":     []string{"123", "456"},
		"age":      1,
		"nickname": "123",
		"form": map[string]interface{}{
			"one":   "1",
			"two":   "2",
			"three": []string{"123", "456"},
			"four": map[string]interface{}{
				"one": "1",
				"two": "2",
			},
		},
	}
	
client.Request().SetParameter(p).GetToString("http://list.com/pass/get.php")
	


//自定义header
header := map[string]string{"user-agent": "Iphone100"}

//添加全局header	
client:=http.Client().SetHeader(header)

//局部header	
re, err :=client.Request().SetHeader(header).GetToString("http://list.com/pass/header.php")


//proxy
client := http.Client()

client.SetProxyAddress("http://127.0.0.1:4780")

html, err := client.Request().GetToString("https://www.google.com/")


//timeout
client := http.Client()

client.SetTimeout(1*time.Second)

html, err := client.Request().GetToString("https://www.google.com/")


//重试次数,默认为0
html,err:=client.Request().SetReTryTimes(3).GetToString("https://xxxccaacasdad.com")




2.时间处理

//时间戳转时间格式,目前仅支持Y、m、d、H、i、s、w
tools.Date("Y-m-d", time.Now().Unix())
tools.Date("Y-m-d H:i:s", time.Now().Unix()))
tools.Date("Y-m-d H:i", time.Now().Unix())
tools.Date("Y", time.Now().Unix())
tools.Date("Ym", time.Now().Unix()))
tools.Date("Ymd", time.Now().Unix()))
tools.Date("H", time.Now().Unix()))
tools.Date("Hi", time.Now().Unix()))

//时间格式转时间戳,单位秒
tools.StrToTime("2020/12/12")
tools.StrToTime("2020-12-12")
tools.StrToTime("2020-12-12 11:32:00")
tools.StrToTime("2020/12/12 11:32:00")




3.数组操作


//in_array
array := []string{"1", "2", "3", "4"}

b := tools.InArray(array, "4")

println(b)

array2 := []int{1, 2, 3, 4}

b2 := tools.InArray(array2, 4)

println(b2)




//implode

arr:=[]string{"1","2","3"}

fmt.Println(tools.Implode("-",arr))





4.文件操作

package main

import (
	"fmt"
	"github.com/PeterYangs/tools/file"
)

func main() {
    
	//一次性读取
	str, err := file.Read("README.md")

	if err != nil {

		fmt.Println(err)

		return
	}

	fmt.Println(string(str))


	//逐行读取
	err := file.ReadLine("README.md", func(line []byte) {

		fmt.Println(string(line))

	})

	if err != nil {

		fmt.Println(err)

		return
	}

	//一次性写入
	file.Write("xx.txt", []byte("123"))
}


5.字符串操作


//explode
str:="1,2,3"

fmt.Println(tools.Explode(",",str))




//md5
str := "123"

fmt.Println(tools.Md5(str))




//字符串截取
str := "我尼玛"

//起始1,长度1
tools.SubStr(str, 1, 1)

//起始字符串长度倒数第二,长度最大
tools.SubStr(str, -2, -1)

//起始0,长度倒数第二
tools.SubStr(str, 0, -2)



6.3des加密

package main

import (
	"fmt"
	"github.com/PeterYangs/tools/secret"
)

func main() {

	d := secret.NewDes()

	source := "hello world"
	fmt.Println("原字符:", source)

	key := d.GenerateKey() //24位

	//加密
	code, err := d.Encyptog3DES([]byte(source), key)

	if err != nil {

		fmt.Println("加密错误", err)

		return
	}

	fmt.Println("密文:", string(code.ToBase64()))

	//解密
	real, err := d.Decrptog3DES(code.ToBase64(), key, secret.Base64)
	//
	if err != nil {

		fmt.Println("解密错误", err)

		return
	}

	fmt.Println("解密:", string(real))

}

7.文件夹操作

package main

import (
	"fmt"
	"github.com/PeterYangs/tools/file"
)

func main() {

	//获取目标文件夹下的所有文件(包含子目录,返回false则不继续遍历)
	file.GetDirList("./", func(path string) bool {

		fmt.Println(path)

		return true
	})

}

8.Hash

package main

import (
	"fmt"
	"github.com/PeterYangs/tools"
)

func main() {

	h := tools.HashHmac([]byte("132"), []byte("456"), false)

	fmt.Println(h)

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToByte added in v0.1.2

func ConvertToByte(src string, srcCode string, targetCode string) []byte

func Date added in v0.0.6

func Date(format string, timestamp int64) string

时间格式函数

func DeleteFile

func DeleteFile(path string) error

删除文件

func DownloadFile

func DownloadFile(url string, path string, setting HttpSetting) error

DownloadFile 下载文件

func DownloadImage added in v0.0.3

func DownloadImage(url string, path string, setting HttpSetting) error

DownloadImage 图片下载

func Explode added in v0.0.4

func Explode(delimiter, text string) []string

func GetExtensionName added in v0.0.11

func GetExtensionName(fileName string) (string, error)

获取文件名拓展名

func GetToBody added in v0.1.1

func GetToBody(url string, setting HttpSetting) (io.ReadCloser, error)

GetToBody 注意要手动关闭body

func GetToResp added in v0.1.1

func GetToResp(url string, setting HttpSetting) (*http.Response, error)

GetToResp 注意要手动关闭body

func GetToString added in v0.1.0

func GetToString(url string, setting HttpSetting) (string, error)

GetToString get获取字符串结果

func GetToStringWithHeader added in v0.2.14

func GetToStringWithHeader(url string, setting HttpSetting) (string, http.Header, error)

GetToStringWithHeader get获取字符串结果并返回头部信息

func HashHmac added in v0.2.52

func HashHmac(data []byte, key []byte, binary bool) string

func Implode added in v0.2.4

func Implode(glue string, pieces []string) string

func InArray added in v0.2.13

func InArray(array interface{}, item interface{}) bool

func In_array added in v0.0.8

func In_array(array interface{}, item interface{}) bool

老版兼容函数

func IsGBK added in v0.1.2

func IsGBK(data []byte) bool

func Join added in v0.2.4

func Join(glue string, pieces []string) string

func Md5 added in v0.0.4

func Md5(str string) string

func MkDirDepth added in v0.0.12

func MkDirDepth(path string) error

创建多级文件夹

func MtRand added in v0.2.13

func MtRand(min, max int64) int64

func Mt_rand added in v0.0.13

func Mt_rand(min, max int64) int64

Mt_rand 老版本兼容函数

func PostToString added in v0.1.0

func PostToString(url string, setting HttpSetting) (string, error)

PostToString post获取字符串结果

func Query

func Query(url string, method string, setting HttpSetting) (*http.Response, error)

Query 请求底层函数

func ReadFile

func ReadFile(path string) (string, error)

读取文件

func SendEmail added in v0.0.5

func SendEmail(from string, to []string, title string, content string, host string, port int, password string) error

发送邮件

func StrPos added in v0.2.40

func StrPos(str, substr string) int

func StrToTime added in v0.0.6

func StrToTime(str string) int64

func SubStr added in v0.2.8

func SubStr(str string, start int, length int) string

SubStr 字符串截取 start为-1则为最后一个,-2则为倒数第二,以此类推 length为-1则为最大长度,-2则倒数第二,以此类推

func WriteLine

func WriteLine(path string, data string)

Types

type HttpSetting added in v0.1.0

type HttpSetting struct {
	TimeOut      int                    //超时时间
	Header       map[string]string      //header
	Parameter    map[string]interface{} //参数
	ProxyAddress string                 //代理地址
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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