Giter Club home page Giter Club logo

toolkit's Introduction

go 工具箱

为了快速使用通用功能,做一次通用封装

使用

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"

toolkit's People

Contributors

eyasliu avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.