1、用go编写的随机数据生成器
开源地址:https://github.com/brianvoe/gofakeit
实例:
package main
import (
"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 main
import (
"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())
// 耗时操作1
func request1()interface{}{
//sql request...
}
// 耗时操作2
func 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 main
import (
"fmt"
"github.com/jesusslim/dtcvt"
)
func main() {
//to string
a := []byte("sa")
r := dtcvt.MustString(a, "gg")
fmt.Println("To string:", r)
//to int
b := 123
r2 := dtcvt.MustInt(b)
fmt.Println("To int:", r2)
//to float
c := []byte("12.7")
r3 := dtcvt.MustFloat64(c)
fmt.Println("To float:", r3)
}
5、JSON格式化
开源地址:https://github.com/json-iterator/go
实例:
package main
import (
"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"`
}
//结构体转JSON
func 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.ConfigCompatibleWithStandardLibrary
json.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 main
import (
"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 main
import (
"log"
"net/http"
"net/http/httputil"
"net/url"
)
var (
// 建立域名和目标map
hostTarget = 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.ReverseProxy
hostProxy 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())
}