为了快速使用通用功能,做一次通用封装
go get -u -v github.com/go-eyas/toolkit
import "github.com/go-eyas/toolkit/http"
github := http.BaseURL("https://api.github.com")
res, err := github.Get("/repos/eyasliu/blog/issues")
var data interface{}
res.JSON(&data)
import "github.com/go-eyas/toolkit/log"
log.Init(&log.Config{})
log.Info("log init ok")
log.Infof("is info log %s %d %v", "string", 123, map[string]string{"test": "hello"})
import "github.com/go-eyas/toolkit/redis"
r, err := redis.Init(&redis.Config{
Cluster: false, // 是否集群
Addrs: []string{"127.0.0.1:6379"}, // redis 地址,如果是集群则在数组上写多个元素
Password: "",
DB: 1,
})
r.Set("tookit:test", `{"hello": "world"}`)
str, err := r.Get("toolkit:test")
import "github.com/go-eyas/toolkit/websocket"
ws := websocket.New(&Config{})
http.HandleFunc("/ws", ws.HTTPHandler)
go func() {
rec := ws.Receive()
for {
req, _ := <-rec
req.Response([]byte("1234556"))
}
}()
http.ListenAndServe("127.0.0.1:8800", nil)
还有一个类似 http api 的开箱即用服务
import "github.com/go-eyas/toolkit/amqp"
mq := amqp.New(*amqp.Config{
Addr: "amqp://guest:[email protected]:5672",
ExchangeName: "toolkit.exchange.test",
})
queue := &amqp.Queue{Name: "toolkit.queue.test"}
err := mq.Pub(queue, &amqp.Message{Data: []byte("{\"hello\":\"world\"}")})
msgch, err := mq.Sub(queue)
for msg := range msgch {
fmt.Printf("%s", string(msg.Data))
}
import "github.com/go-eyas/toolkit/config"
conf := struct {
Host string
Port int
}{}
config.Init("config", &conf)
import "github.com/go-eyas/toolkit/db"
var db *gorm.DB = db.Gorm(&db.Config{"mysql", "username:[email protected]:3306/test"})
var db *xorm.Engine = db.Xorm(&db.Config{"mysql", "username:[email protected]:3306/test"})
defer db.Close()
资源自动 crud
import "github.com/go-eyas/toolkit/db"
import "github.com/go-eyas/toolkit/db/resource"
type Article struct {
ID int64 `resource:"pk;search:none"`
Title string `resource:"create;update;search:like"`
Status byte `resource:"search:="`
}
var r, db, err = resource.New(&db.Config{"mysql", "username:[email protected]:3306/test"}, Article{})
r.Create(map[string]string{"title": "hello eyas"}) // 增
r.Delete(1) // 删
r.Update(1, map[string]int{"status": 1}) // 改
// 查,指定主键
var m = &Article{}
r.Detail(1, m)
// 查,指定查询条件查列表
var list = []*Article{}
r.List(&list, map[string]interface{}{"title": "he"}, []string{"id DESC"})
import "github.com/go-eyas/toolkit/gin/util" // 工具函数
import "github.com/go-eyas/toolkit/gin/middleware" // 中间件
import "github.com/go-eyas/toolkit/emit"
fn1 := func(data interface{}) {
fmt.Printf("fn1 receive data: %v", data)
}
emit.On("evt", fn1).Off("evt", fn1)
emit.Emit("evt", "hello emitter")
import (
"github.com/go-eyas/toolkit/email"
"github.com/BurntSushi/toml"
)
func ExampleSample() {
tomlConfig := `
host = "smtp.qq.com"
port = "465"
account = "[email protected]"
password = "haha, wo cai bu gao su ni ne"
name = "unit test"
secure = true
[tpl.a]
bcc = ["Jeason <[email protected]>"] # 抄送
cc = [] # 抄送人
subject = "Welcome, {{.Name}}" # 主题
text = "Hello, I am {{.Name}}" # 文本
html = "<h1>Hello, I am {{.Name}}</h1>" # html 内容
`
conf := &Config{}
toml.Decode(tomlConfig, conf)
email := New(conf)
email.SendByTpl("Yuesong Liu <[email protected]>", "a", struct{ Name string }{"Batman"})
}
import "github.com/go-eyas/toolkit/util"