1
0
Fork 0
Personal collection of Go packages that I wish were in the standard library
Go to file
Gregory Eremin 59a63295b0 Add csv2 package that can unmarshal into structures 2018-07-03 19:48:26 +02:00
config Use file contents for config test 2018-07-03 19:46:54 +02:00
csv2 Add csv2 package that can unmarshal into structures 2018-07-03 19:48:26 +02:00
filecache Stuff 2018-06-24 16:36:44 +02:00
log Add set docs 2018-06-24 03:06:28 +02:00
ptrto Add ptrto package that provides pointers to basic types 2018-07-03 19:47:48 +02:00
reflect2 Extract column indexing into reflect2 package 2018-07-03 19:47:23 +02:00
set Add context to logs, update readme 2018-06-24 02:57:14 +02:00
sqldb Extract column indexing into reflect2 package 2018-07-03 19:47:23 +02:00
threadpool Add thread pool implementation 2018-06-17 12:57:47 +02:00
time2 Add time2 package with time freeze feature 2018-07-03 19:48:07 +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 Stuff 2018-06-24 16:36:44 +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"
ctx := context.Background()
var items []Item
filecache.Load(&items, "tmp/cache/items.json", func() {  
    err := conn.Query(ctx, "SELECT * FROM items").Load(&items).Error()
    if err != nil {
        log.Fatalf("Failed to load items: %v", err)
    }
})

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 

Set

There is a collection of packages implementing a set data type located inside the set package. Implemented types include:

  • int, int8, int16, int32, int64
  • uint, uint8, uint16, uint32, uint64
  • string

All the package names are type names prefixed with "set", e.g. setuint64.

import "github.com/localhots/gobelt/set/setstring"
s := setstring.New("one", "two")
s.Add("three")
s.Remove("one", "two").Add("four", "five")
fmt.Println("Size:", s.Len()) // 3
fmt.Println("Has one", s.Has("one")) // false
fmt.Println(s.SortedSlice()) // [three four five]

Config

import "github.com/localhots/gobelt/config"

Describe configuration structure inside a target package.

package db

var conf struct {
    Flavor string `toml:"flavor"`
    DSN    string `toml:"dsn"`
}

func init() {
    config.Require("db", &conf)
}

Load configuration from a main function:

package main

func main() {
    config.Load("config/config.toml")
}