Gokit is a toolkit written by Go (Golang).It aims to speed up the development.
To install Gokit package, you need to install Go and set your Go workspace first.
- The first need Go installed (version 1.21+ is required), then you can use the below Go command to install Gokit.
$ go install github.com/neo532/gokit
- Import it in your code:
import "github.com/neo532/gokit"
It is a distributed lock with signle instance by redis.
package main
import (
"github.com/go-redis/redis/v8"
"github.com/neo532/gokit/util"
)
type RedisOne struct {
cache *redis.Client
}
func (l *RedisOne) Eval(c context.Context, cmd string, keys []string, args []interface{}) (interface{}, error) {
return l.cache.Eval(c, cmd, keys, args...).Result()
}
var Lock *util.Lock
func init(){
rdb := &RedisOne{
redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "password",
})
}
Lock = util.NewLock(rdb)
}
func main() {
c := context.Background()
key := "IamAKey"
expire := time.Duration(10) * time.Second
wait := time.Duration(2) * time.Second
code, err := Lock.Lock(c, key, expire, wait)
Lock.UnLock(c, key, code)
}
It is a frequency with signle instance by redis.
package main
import (
"github.com/go-redis/redis/v8"
"github.com/neo532/gokit/util"
)
type RedisOne struct {
cache *redis.Client
}
func (l *RedisOne) Eval(c context.Context, cmd string, keys []string, args []interface{}) (interface{}, error) {
return l.cache.Eval(c, cmd, keys, args...).Result()
}
var Freq *util.Freq
func init(){
rdb := &RedisOne{
redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "password",
})
}
Freq = util.NewFreq(rdb)
Freq.Timezone("Local")
}
func main() {
c := context.Background()
preKey := "user.test"
rule := []util.FreqRule{
util.FreqRule{Duri: "10000", Times: 80},
util.FreqRule{Duri: "today", Times: 5},
}
fmt.Println(Freq.IncrCheck(c, preKey, rule...))
fmt.Println(Freq.Get(c, preKey, rule...))
}
It is a tool to page slice.
package main
import (
"github.com/neo532/gokit/util"
)
func main() {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
// [1 2 3] [4 5 6] [7 8 9] [10]
err := PageExec(int64(len(arr)), 3, func(b, e int64, p int) (err error) {
fmt.Println(arr[b:e])
return
})
}
It is a tool to exec goroutine safely.
package main
import (
"github.com/neo532/gokit/gofunc"
)
func main() {
fn := func(i int) (err error) {
// do something...
return
}
log := &gofunc.DefaultLogger{}
gofn := gofunc.NewGoFunc(gofunc.WithLogger(log), gofunc.WithMaxGoroutine(20))
l := 1000000
fns := make([]func(i int) error, 0, l)
for i := 0; i < l; i++ {
fns = append(fns, fn)
}
gofn.WithTimeout(c, time.Second*2, fns...)
err := log.Err()
}
It is a highly scalable logger.
package main
import (
"github.com/neo532/gokit/logger"
)
func main() {
c := context.Background()
var l logger.Logger
l = newSlog() // more detail in test file
l = newZap() // more detail in test file
l.WithArgs(logger.KeyModule, "db").Info(c, "msg1", "err", "panic")
l.WithArgs(logger.KeyModule, "queue").WithLevel(logger.LevelError).Info(c, "m2", "e1", "p1")
l.WithArgs(logger.KeyModule, "redis").Errorf(c, "m%s", "3")
}
A well-encapsulated GORM that can support shadow databases, hot configuration updates, master-slave separation, high scalability, simplicity, and domain-layer transactions.
package main
import (
"github.com/neo532/gokit/database/orm"
)
func main() {
db, clean, err := initDB() // more detail in test file
defer clean()
if err != nil {
return
}
c := context.Background()
err = db.Transaction(c, func(c context.Context) (err error) {
var databases []string
if err = dbs.Write(c).Raw("show databases").Scan(&databases).Error; err != nil {
return
}
if err = dbs.Read(c).Raw("show databases").Scan(&databases).Error; err != nil {
return
}
return
})
}
A well-encapsulated Redis client that can support shadow databases, hot configuration updates, gray environment, high scalability and simplicity.
// more detail in test file
A message queue client with high scalability that supports the full-link connection between producers and consumers and also supports customizable middleware.
// more detail in test file