Golang的插件库

1,文件上传

github.com/julienschmidt/httprouter

  

2,高效读取配置信息

gopkg.in/ini.v1

  

3,实时读取文件内容

github.com/hpcloud/tail

 

4,加密

1,哈希算法
golang.org/x/crypto/bcrypt

2,md5 
crypto/md5

3,base64
encoding/base64

4,sha
crypto/sha1

5,hmac
crypto/hmac

  

5,跨域

package main

import (
    "net/http"
)

funccors(f http.HandlerFunc)http.HandlerFunc {
    returnfunc(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Access-Control-Allow-Origin", "*")  // 允许访问所有域,可以换成具体url,注意仅具体url才能带cookie信息
        w.Header().Add("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token") //header的类型
        w.Header().Add("Access-Control-Allow-Credentials", "true") //设置为true,允许ajax异步请求带cookie信息
        w.Header().Add("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") //允许请求方法
        w.Header().Set("content-type", "application/json;charset=UTF-8")             //返回数据格式是jsonif r.Method == "OPTIONS" {
            w.WriteHeader(http.StatusNoContent)
            return
        }
        f(w, r)
    }
}
funcindex(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("Hello Golang"))
}
funcmain() {
    http.HandleFunc("/", cors(index))
    http.ListenAndServe(":8000", nil)
}

  

6,系统性能数据gopsutil 库

go get github.com/shirou/gopsutil

  

7,pprof 性能调优

import "runtime/pprof"

pprof.StartCPUProfile(w io.Writer)

pprof.StopCPUProfile()

  

8,验证器

go get github.com/go-ozzo/ozzo-validation/v3
go get github.com/go-ozzo/ozzo-validation/v3/is

  

9,验证码

go get github.com/dchest/captcha

  

10,发邮件

go get github.com/jordan-wright/email

  

11,文件监控

github.com/fsnotify/fsnotify

  

12,生成PDF

go get github.com/jung-kurt/gofpdf

  

13,获取http请求的IP地址

package main

import (
    "encoding/json"
    "net/http"
)

func main() {
    http.HandleFunc("/", ExampleHandler)
    if err := http.ListenAndServe(":8080", nil); err != nil {
        panic(err)
    }
}

func ExampleHandler(w http.ResponseWriter, r *http.Request) {
    w.Header().Add("Content-Type", "application/json")
    resp, _ := json.Marshal(map[string]string{
        "ip": GetIP(r),
    })
    w.Write(resp)
}

// GetIP gets a requests IP address by reading off the forwarded-for
// header (for proxies) and falls back to use the remote address.
func GetIP(r *http.Request) string {
    forwarded := r.Header.Get("X-FORWARDED-FOR")
    if forwarded != "" {
        return forwarded
    }
    return r.RemoteAddr
}

  

14,压缩解压文件

archive/zip

  

15,PDF 转JPG

sudo apt install libmagic-dev libmagickwand-dev

go get gopkg.in/gographics/imagick.v2/imagick

  

16,Sessions

github.com/gorilla/sessions

  

17,markdown 解析库

 import github.com/russross/blackfriday

  

18,gjson 可以方便的从一个json串中读取值

go get github.com/tidwall/gjson

  

19,解决中文乱码

go get github.com/axgle/mahonia
package main

import "fmt"
import "github.com/axgle/mahonia"

func main() {
    enc := mahonia.NewEncoder("gbk")
    //converts a  string from UTF-8 to gbk encoding.
    fmt.Println(enc.ConvertString("hello,世界"))
}

  

20,二维码

go get github.com/skip2/go-qrcode

import qrcode "github.com/skip2/go-qrcode"

  

21,Yaml编码和解码

gopkg.in/yaml.v2

  

22,权限管理

Casbin的作用:

以经典{subject, object, action}形式或您定义的自定义形式实施策略,同时支持允许和拒绝授权。
处理访问控制模型及其策略的存储。
管理角色用户映射和角色角色映射(RBAC中的角色层次结构)。
支持内置的超级用户,例如root或administrator。超级用户可以在没有显式权限的情况下执行任何操作。
多个内置运算符支持规则匹配。例如,keyMatch可以将资源键映射/foo/bar到模式/foo*。
Casbin不执行的操作:

身份验证(又名验证username以及password用户登录时)
管理用户或角色列表。我相信项目本身管理这些实体会更方便。用户通常具有其密码,而Casbin并非设计为密码容器。但是,Casbin存储RBAC方案的用户角色映射。

go get github.com/casbin/casbin

  

23,Swaggo 生成api文档

go get -u github.com/swaggo/swag/cmd/swag

  

24,网页截图

github.com/chromedp/chromedp

  

25,限流器

golang.org/x/time/rate

  

26,缓存

github.com/patrickmn/go-cache

  

27,支付插件

go get github.com/iGoogle-ink/gopay

  

28,获取服务器配置

    "github.com/shirou/gopsutil/cpu"
    "github.com/shirou/gopsutil/disk"
    "github.com/shirou/gopsutil/load"
    "github.com/shirou/gopsutil/mem"

  

29,热重启

go get -u github.com/cosmtrek/air

在项目根目录创建一个名为 .air.conf 的配置文件

创建完毕之后,在文件中写入你应用运行的命令如
go build main.go

运行项目只需要在项目根目录执行如下命令:

air -c .air.conf

  

30,设置https

github.com/unrolled/secure

  

posted @ 2021-03-24 13:04  pebblecome  阅读(750)  评论(0)    收藏  举报