Skip to content

Instantly share code, notes, and snippets.

@hauxe
hauxe / worker_pool.go
Last active May 27, 2018 05:33
Worker pool
package main
// Job define job interface
type Job interface {
Execute()
}
// JobQueue queue of jobs
var JobQueue chan Job
@hauxe
hauxe / pool_of_workers.go
Last active July 5, 2023 15:03
Pool of Workers
package main
import (
"sync"
"time"
)
// Job define job interface
type Job interface {
Execute()
@hauxe
hauxe / fileio.go
Created May 29, 2018 09:46
file io pipeline and broadcast
package main
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"sync"
@hauxe
hauxe / crud_struct.go
Created June 3, 2018 05:50
crud structure
// Config defines crud properties
type Config struct {
DB *sqlx.DB
TableName string
Object Object
L bool
C bool
R bool
U bool
D bool
@hauxe
hauxe / crud_object_interface.go
Created June 3, 2018 06:00
crud object interface
// Object target crud object
type Object interface {
Get() interface{}
}
@hauxe
hauxe / crud_validator_type.go
Created June 3, 2018 06:16
crud validator type
type Validator func(method string, obj interface{}) error
@hauxe
hauxe / crud_register_create.go
Created June 3, 2018 06:24
crud register create handler
func (crud *CRUD) registerC() gomHTTP.ServerRoute {
// build create sql
fields := crud.Config.createFields
if len(fields) == 0 {
// allow create all fields
fields = crud.Config.fields
}
fieldNames := make([]string, len(fields))
for i, field := range fields {
fieldNames[i] = field.name
@hauxe
hauxe / crud_create_function.go
Created June 3, 2018 06:27
crud create function
// Create creates from map
func (crud *CRUD) Create(data interface{}) error {
// build fields
result, err := crud.Config.DB.NamedExec(crud.Config.sqlCRUDCreate, data)
if err != nil {
return errors.Wrap(err, "error crud create")
}
// set primary key
rv := reflect.ValueOf(data)
rv = reflect.Indirect(rv)
@hauxe
hauxe / crud_register_read.go
Created June 10, 2018 06:49
crud register read handler
func (crud *CRUD) registerR() gomHTTP.ServerRoute {
// build select sql
fields := crud.Config.selectFields
if len(fields) == 0 {
// allow select all fields
fields = crud.Config.fields
}
fieldNames := make([]string, len(fields))
for i, field := range fields {
fieldNames[i] = field.name
@hauxe
hauxe / crud_read_function.go
Created June 10, 2018 06:54
crud read function
// Read read data
func (crud *CRUD) Read(data interface{}) (interface{}, error) {
rv := reflect.ValueOf(data)
rv = reflect.Indirect(rv)
pk := rv.Field(crud.Config.pk.index)
if !pk.CanInterface() {
return nil, errors.Errorf("table %s with primary key has wrong interface type",
crud.Config.TableName, crud.Config.pk.index)
}
rows, err := crud.Config.DB.Queryx(crud.Config.sqlCRUDRead, pk.Interface())