刚写GO没多久,记录一些用过的小方法,方便以后用的时候直接复制
一、http请求
import (
"crypto/tls"
"io"
"io/ioutil"
"net/http"
)
func HttpRequest(api string, method string, body io.Reader) ([]byte, error) {
//设置忽略tls证书验证
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
request, err := http.NewRequest(method, api, body)
if err != nil {
return nil, err
}
if method == "POST" {
request.Header.Set("Content-Type", "application/json; charset=utf-8")
}
if method == "PATCH" {
request.Header.Set("Content-Type", "application/merge-patch+json")
}
response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()
reader, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return reader, nil
}
二、获取数组的key
func GetKeys(m map[string]string) []string {
// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率很高
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
三、生成随机数
import (
"fmt"
"math/rand"
)
func GetRandom(len int) string {
b := make([]byte, len)
rand.Read(b)
return fmt.Sprintf("%x", b)
}
四、解压zip
import (
"archive/zip"
"io"
"os"
"path/filepath"
)
func Unzip(zipFile string) error {
zipReader, err := zip.OpenReader(zipFile)
if err != nil {
return err
}
defer zipReader.Close()
for _, f := range zipReader.File {
fPath := filepath.Join(zipFile, f.Name)
if f.FileInfo().IsDir() {
_ = os.MkdirAll(fPath, os.ModePerm)
} else {
if err = os.MkdirAll(filepath.Dir(fPath), os.ModePerm); err != nil {
return err
}
inFile, err := f.Open()
if err != nil {
return err
}
outFile, err := os.OpenFile(fPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
_, err = io.Copy(outFile, inFile)
if err != nil {
return err
}
outFile.Close()
inFile.Close()
}
}
return nil
}
五、解压zar.gz
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path"
)
func UnCompressTarGz(srcFilePath string, destDirPath string) error {
fr, err := os.Open(srcFilePath)
if err != nil {
return err
}
defer fr.Close()
gr, err := gzip.NewReader(fr)
if err != nil {
return err
}
defer gr.Close()
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
if hdr.Typeflag != tar.TypeDir {
_ = os.MkdirAll(destDirPath+"/"+path.Dir(hdr.Name), os.ModePerm)
fw, err := os.OpenFile(destDirPath+"/"+hdr.Name, os.O_CREATE|os.O_WRONLY, os.FileMode(hdr.Mode))
if err != nil {
return err
}
_, err = io.Copy(fw, tr)
if err != nil {
return err
}
}
}
return nil
}
六、md5加密
import (
"crypto/md5"
"fmt"
)
func Md5(str string) string {
data := []byte(str)
has := md5.Sum(data)
return fmt.Sprintf("%x", has) //将[]byte转成16进制
}
七、判断目录或者文件是否存在
import (
"os"
)
func IsExistPath(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}
八、运行shell命令
import (
"os/exec"
)
func RunCommand(command string) (output []byte, err error) {
var cmd *exec.Cmd
cmd = exec.Command("/bin/sh", "-c", command)
output, err = cmd.Output()
return output, err
}