Skip to content

Instantly share code, notes, and snippets.

@lummie
lummie / go_channel_simple_worker_pattern.go
Created Sep 28, 2021
Go channel based Simple Worker pattern
View go_channel_simple_worker_pattern.go
package main
import (
"fmt"
"log"
"sync"
)
/*
This is an example of a worker pattern for channels
@lummie
lummie / throttle_concurrent_work.go
Created May 3, 2020
Simple pattern using a channel to throttle the number of concurrent workers
View throttle_concurrent_work.go
// change the channel size to the number of concurrent tasks you'd like
var throttle = make(chan struct{}, 1)
func DoStuff() {
// block until we can add to throttle channel
throttle <- struct{}{}
defer func() {
<-throttle // remove from channel when we are done
}()
// do your funky stuff
@lummie
lummie / custom_time.go
Last active Dec 29, 2021
Golang custom date / time formats when marshalling to JSON
View custom_time.go
// CustomTime provides an example of how to declare a new time Type with a custom formatter.
// Note that time.Time methods are not available, if needed you can add and cast like the String method does
// Otherwise, only use in the json struct at marshal/unmarshal time.
type CustomTime time.Time
const ctLayout = "2006-01-02 15:04:05 Z07:00"
// UnmarshalJSON Parses the json string in the custom format
func (ct *CustomTime) UnmarshalJSON(b []byte) (err error) {
@lummie
lummie / FileServerWith404.go
Last active Dec 26, 2021
A wrapper for GOs http.FileServer that allows a custom 404 handler to be assigned
View FileServerWith404.go
package middleware
import (
"net/http"
"os"
"path"
"strings"
)
// FSHandler404 provides the function signature for passing to the FileServerWith404
@lummie
lummie / extend_struct_json.go
Created Jul 25, 2017
Idiomatic way to extend a struct when marshalling to Json
View extend_struct_json.go
package main
// This example shows how to extend a struct for marshalling, e.g. to Json to allow private fields
// to be exposed or for adding additonal fields that are populated at runtime.
import (
"encoding/json"
"fmt"
"time"
)
@lummie
lummie / string_test.go
Created Jun 8, 2017
Golang Test Empty string vs len
View string_test.go
package string_test
import "testing"
func Benchmark_StringNotEqual(b *testing.B) {
z := ""
i := 0
for i:= 0; i<b.N; i++ {
if z != "" {
i++
@lummie
lummie / generic_channel.go
Created Apr 27, 2017
Go Example - Channel that can receive any type
View generic_channel.go
package main
import (
"log"
"reflect"
)
/*
This is an example of a channel that can receive any data type, e.g. different struct types
@lummie
lummie / enum.go
Last active Jan 16, 2022
Golang Enum pattern that can be serialized to json
View enum.go
package enum_example
import (
"bytes"
"encoding/json"
)
// TaskState represents the state of task, moving through Created, Running then Finished or Errorred
type TaskState int
@lummie
lummie / gist:732b8cb0b478966b91a7f9244c7aeac7
Last active Dec 12, 2021
Change exif timezone with exiftool -8 hour
View gist:732b8cb0b478966b91a7f9244c7aeac7
// I'm forever forgetting to adjust the time zone on my camera when taking pictures on vacation
// this uses the linux exiftool to adjust the timezone -8 hours for all images in a directory
exiftool "-DateTimeOriginal-=0:0:0 8:0:0" *
@lummie
lummie / GoSublime.sublime-settings
Created Nov 30, 2015
GoSublime User settings for build / test on Save
View GoSublime.sublime-settings
{
"on_save": [{
"cmd": "gs9o_run_many", "args": {
"commands":[
["clear"],
["sh", "if [ -f onsave.sh ]; then ./onsave.sh; else gofmt -s -w ./ && go build . errors && go test -i && go test && go vet && golint ; fi"]
],
"focus_view": false
}
}],