2018-06-17 13:56:44 +02:00
2018-06-24 02:57:14 +02:00
2018-06-24 02:57:14 +02:00
2018-06-23 23:46:35 +02:00
2018-06-17 12:57:47 +02:00
2018-06-17 12:57:47 +02:00
2018-06-23 23:46:35 +02:00
2018-06-24 02:57:14 +02:00

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 
Description
Personal collection of Go packages that I wish were in the standard library
Readme 80 KiB
Languages
Go 99.7%
Makefile 0.3%