Gregory Eremin 01c445213c | ||
---|---|---|
filecache | ||
log | ||
set | ||
sqldb | ||
threadpool | ||
LICENSE | ||
Makefile | ||
README.md |
README.md
Gobelt
Gobelt is a collection of Go tools.
Thread pool
import "github.com/localhots/gobelt/threadpool"
ctx := context.Background()
ctx, cancel = context.WithTimeout(ctx, 30 * time.Second)
defer cancel()
pool := threadpool.New(10)
defer pool.Close()
for i := 0; i < 1000000; i++ {
i := i
pool.Enqueue(ctx, func() {
fmt.Printf("The number is %d\n", i)
})
}
File cache
import "github.com/localhots/gobelt/filecache"
var val int
filecache.Load(&val, "path/to/cachefile", func() interface{} {
var items []Item
err := conn.Query(ctx, "SELECT * FROM items").Load(&items).Error()
if err != nil {
log.Fatal("Failed to load items", log.F{"error": err})
}
return items
})
Log
import "github.com/localhots/gobelt/log"
ctx := context.Background()
ctx = log.ContextWithFields(ctx, log.F{"email": params["email"]})
user, err := signup(ctx, params)
if err != nil {
log.Errorf(ctx, "Signup failed: %v", err)
// [ERRO] Signup failed: db: duplicate entry email=bob@example.com
return
}
log.Info(ctx, "New user signed up", log.F{"id": user.ID})
// [INFO] New user signed up email=bob@example.com id=14