Skip to content

Instantly share code, notes, and snippets.

@lummie
lummie / exit.go
Created September 21, 2023 17:27
Wait for graceful exit.. [k8s, pod, cli]
/*
Waits for a terminate | interrupt signal, ctrl-c before finishing main
when creating services and adapters, I like to return a cleanup function that is deferred for
each service, thus when the app closes they are teared down in reverse order.
graceful.Exit() returns two functions:
- waitFor - which is called as the last statement of main. This will block until a signal to terminate or interrupt the executable.
- deferMe - defer this first before the service cleanups. This ensures thar stdout/err are flushed and sleeps 1 second before exiting.
@lummie
lummie / .bashrc
Last active July 14, 2022 06:49
k8s, git, full path PS1 bash prompt
# install kube-ps1 from https://github.com/jonmosco/kube-ps1
alias sn="kubectl config set-context --current --namespace" # switch namespace
# setup bash prompt
source "/usr/local/opt/kube-ps1/share/kube-ps1.sh" && k8s='$(kube_ps1)' # k8s cluster
export PS1="\n\n 🦔" # headgehog
export PS1="$PS1\n┏━━━━━┯━ 🕰 \t ━━━━━━━━━━━━━━━━━━━━━━━━━" # headgehog
export PS1="$PS1\n┃ git │ \$(git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/')" # git branch
export PS1="$PS1\n┃ k8s │ $k8s" # k8s info
@lummie
lummie / go_channel_simple_worker_pattern.go
Created September 28, 2021 14:59
Go channel based Simple Worker pattern
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 13:56
Simple pattern using a channel to throttle the number of concurrent workers
// 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 August 14, 2024 14:14
Golang custom date / time formats when marshalling to JSON
// 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 August 9, 2024 06:31
A wrapper for GOs http.FileServer that allows a custom 404 handler to be assigned
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 July 25, 2017 17:04
Idiomatic way to extend a struct when marshalling to Json
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 June 8, 2017 07:53
Golang Test Empty string vs len
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 April 27, 2017 07:47
Go Example - Channel that can receive any type
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 September 16, 2024 18:03
Golang Enum pattern that can be serialized to json
package enum_example
import (
"bytes"
"encoding/json"
)
// TaskState represents the state of task, moving through Created, Running then Finished or Errorred
type TaskState int