Skip to content

Instantly share code, notes, and snippets.

@hauxe
hauxe / generic_env_test.go
Created October 24, 2022 08:44
Generic get environment unit test
package utils
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestGetEnvT(t *testing.T) {
@hauxe
hauxe / generic_env.go
Created October 24, 2022 08:43
Generic get environment
package utils
import (
"os"
"strconv"
)
type Env[T any] struct {
DefaultVal T
FromString func(string) (T, error)
// AND function combines all signal from channels into a single channel with And condition
func AND(channels ...<-chan interface{}) <-chan interface{} {
switch len(channels) {
case 0:
return nil
case 1:
return channels[0]
}
andDone := make(chan interface{})
collector := make(chan interface{}, len(channels))
// OR function combines all signal from channels into a single channel with Or condition
func OR(channels ...<-chan interface{}) <-chan interface{} {
switch len(channels) {
case 0:
return nil
case 1:
return channels[0]
}
orDone := make(chan interface{})
go func() {
package main
import (
"log"
"sync"
)
func main() {
cond := sync.NewCond(&sync.Mutex{})
var wg1 sync.WaitGroup
package main
import (
"flag"
"log"
"net/http"
"strings"
)
// FileSystem custom file system handler
// FileSystem custom file system handler
type FileSystem struct {
fs http.FileSystem
}
// Open opens file
func (fs FileSystem) Open(path string) (http.File, error) {
f, err := fs.fs.Open(path)
if err != nil {
return nil, err
package main
import (
"flag"
"log"
"net/http"
"strings"
)
func main() {
// List lists data and paging the result
func (crud *CRUD) List(pageID, perPage int64) ([]interface{}, error) {
offset := (pageID - 1) * perPage
rows, err := crud.Config.DB.Queryx(crud.Config.sqlCRUDList, offset, perPage)
if err != nil {
return nil, errors.Wrap(err, "error crud list")
}
defer rows.Close()
result := []interface{}{}
func (crud *CRUD) registerL() gomHTTP.ServerRoute {
// build list sql
fields := crud.Config.listFields
if len(fields) == 0 {
// allow update all fields
fields = crud.Config.fields
}
fieldNames := make([]string, len(fields))
for i, field := range fields {
fieldNames[i] = field.name