1
0
Fork 0
Personal collection of Go packages that I wish were in the standard library
Go to file
Gregory Eremin 01c445213c
Add context to logs, update readme
2018-06-24 02:57:14 +02:00
filecache Add examples to readme 2018-06-17 13:56:44 +02:00
log Add context to logs, update readme 2018-06-24 02:57:14 +02:00
set Add context to logs, update readme 2018-06-24 02:57:14 +02:00
sqldb Add sqldb and set packages 2018-06-23 23:46:35 +02:00
threadpool Add thread pool implementation 2018-06-17 12:57:47 +02:00
LICENSE Add thread pool implementation 2018-06-17 12:57:47 +02:00
Makefile Add sqldb and set packages 2018-06-23 23:46:35 +02:00
README.md Add context to logs, update readme 2018-06-24 02:57:14 +02:00

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