1、用go编写的随机数据生成器
开源地址:https://github.com/brianvoe/gofakeit
实例:
package mainimport ("fmt""github.com/brianvoe/gofakeit/v6")//主函数 程序的入口func main() {fmt.Println(gofakeit.JobTitle())fmt.Println(gofakeit.Phone())fmt.Println(gofakeit.CurrencyShort())}
2、Excel工具包
开源地址:https://gitee.com/xurime/excelize
实例:
package mainimport ("fmt""github.com/360EntSecGroup-Skylar/excelize/v2")func main() {f := excelize.NewFile()// 创建一个工作表index := f.NewSheet("Sheet2")// 设置单元格的值f.SetCellValue("Sheet2", "A2", "Hello world.")f.SetCellValue("Sheet1", "B2", 100)// 设置工作簿的默认工作表f.SetActiveSheet(index)// 根据指定路径保存文件if err := f.SaveAs("Book1.xlsx"); err != nil {fmt.Println(err)}}
3、一种异步处理能力
开源地址:https://gitee.com/freshcn/async
实例:
// 建议程序开启多核支持runtime.GOMAXPROCS(runtime.NumCPU())// 耗时操作1func request1()interface{}{//sql request...}// 耗时操作2func request2()interface{}{//sql request...}// 新建一个async对象async:=new async.New()// 添加request1异步请求,第一个参数为该异步请求的唯一logo,第二个参数为异步完成后的回调函数,回调参数类型为func()interface{}async.Add("request1",request1)// 添加request2异步请求async.Add("request2",request2)// 执行if chans,ok := async.Run();ok{// 将数据从通道中取回,取回的值是一个map[string]interface{}类型,key为async.Add()时添加的logo,interface{}为该logo回调函数返回的结果res := <-chans// 这里最好判断下是否所有的异步请求都已经执行成功if len(res) == 2 {for k, v := range res {//do something}} else {log.Println("async not execution all task")}}// 清除掉本次操作的所以数据,方便后续继续使用async对象async.Clean()
4、数据转换
开源地址:https://gitee.com/JesusSlim/dtcvt
实例:
package mainimport ("fmt""github.com/jesusslim/dtcvt")func main() {//to stringa := []byte("sa")r := dtcvt.MustString(a, "gg")fmt.Println("To string:", r)//to intb := 123r2 := dtcvt.MustInt(b)fmt.Println("To int:", r2)//to floatc := []byte("12.7")r3 := dtcvt.MustFloat64(c)fmt.Println("To float:", r3)}
5、JSON格式化
开源地址:https://github.com/json-iterator/go
实例:
package mainimport ("encoding/json""fmt")import jsoniter "github.com/json-iterator/go"//用户type User struct {UserName string `json:"username"`NickName string `json:"nickname"`Age int `json:"age"`Birthday string `json:"birthday"`Sex string `json:"sex"`Email string `json:"email"`Phone string `json:"phone"`}//结构体转JSONfunc structToJSON() {user := User{UserName: "itbsl",NickName: "jack",Age: 18,Birthday: "2001-08-15",Sex: "itbsl@gmail.com",Phone: "176XXXX6688",}data, err := json.Marshal(user)var json = jsoniter.ConfigCompatibleWithStandardLibraryjson.Unmarshal(input, &user)if err != nil {fmt.Println("json.marshal failed, err:", err)return}fmt.Printf("%s\n", string(data))}func main() {structToJSON()}
6、农历
开源地址:https://github.com/6tail/lunar-go
实例:
package mainimport ("fmt""github.com/6tail/lunar-go/calendar")func main() {lunar := calendar.NewLunarFromYmd(1986,4,21)fmt.Println(lunar.ToFullString())fmt.Println(lunar.GetSolar().ToFullString())}
7、反向代理
实例:
package mainimport ("log""net/http""net/http/httputil""net/url")var (// 建立域名和目标maphostTarget = map[string]string{"app1.domain1.com": "http://192.168.1.2/owncloud","app2.domain1.com": "http://192.168.1.2:8080","app3.domain1.com": "http://192.168.1.2:8888",}// 用于缓存 httputil.ReverseProxyhostProxy map[string]*httputil.ReverseProxy)type baseHandle struct{}func (h *baseHandle) ServeHTTP(w http.ResponseWriter, r *http.Request) {host := r.Host// 直接从缓存取出if fn, ok := hostProxy[host]; ok {fn.ServeHTTP(w, r)return}// 检查域名白名单if target, ok := hostTarget[host]; ok {remoteUrl, err := url.Parse(target)if err != nil {log.Println("target parse fail:", err)return}proxy := httputil.NewSingleHostReverseProxy(remoteUrl)hostProxy[host] = proxy // 放入缓存proxy.ServeHTTP(w, r)return}w.Write([]byte("403: Host forbidden " + host))}func main() {h := &baseHandle{}http.Handle("/", h)server := &http.Server{Addr: ":8082",Handler: h,}log.Fatal(server.ListenAndServe())}
