Skip to content

Instantly share code, notes, and snippets.

View komuw's full-sized avatar

Komu Wairagu komuw

View GitHub Profile
@komuw
komuw / encrypt_decrypt.go
Last active July 6, 2022 19:32
encrypt and decrypt in Go
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
@komuw
komuw / erasure_encoding.go
Last active August 3, 2023 08:40
use erasure encoding to survive data loss
package main
import (
"fmt"
"os"
"github.com/klauspost/reedsolomon"
)
// You can use erasure encoding to;
@komuw
komuw / check_mux.go
Last active November 23, 2021 11:07
golang muxes do not inherit middlewares. Also middlewares are not deduped.
package main
import (
"fmt"
"net/http"
"github.com/go-chi/chi"
"github.com/gorilla/mux"
)
@komuw
komuw / cert-manager.yaml
Created September 16, 2021 07:40
create a golang k8(kubernetes) struct from yaml file
# gotten from; https://github.com/jetstack/cert-manager/releases/download/v1.3.3/cert-manager.yamlhttps://github.com/jetstack/cert-manager/releases/download/v1.3.3/cert-manager.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: cert-manager
app.kubernetes.io/component: controller
app.kubernetes.io/instance: cert-manager
app.kubernetes.io/name: cert-manager
name: cert-manager
@komuw
komuw / three_sigma_sampling.py
Last active December 4, 2020 10:18
In a normal distribution, 99.7% of the values are not anomalous. Which means you can throw away 99.7% without losing context. This is useful in sampling.
def sample_anomalous(samples):
"""
Sampling:
- Central limit theorem(Alan Turing)
- 3-sigma rule: In a normal distribution, 99.7% of the values are not anomalous.
Which means you can throw away 99.7% without losing context.
- You may also use 2-sigma(95%) or 1-sigma(68%)
see:
1. https://youtu.be/inrqE0Grgk0?t=26080 (Emmanuel T Odeke)
2. https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule
@komuw
komuw / variable_shadowing_race.go
Created November 5, 2020 13:24
Golang variable shadowing can mask race conditions
package main
import "time"
type cool struct{}
func (c *cool) add() error {
return nil
}
@komuw
komuw / complex_types.go
Last active October 29, 2020 13:29
pointer receiver versus value receiver benchmarks
package main
import (
"fmt"
"math"
"testing"
)
/* ---------- package ---------- */
type h struct {
@komuw
komuw / fdatasync_vs_fsync.go
Last active July 7, 2021 15:32
Benchmark fdatasync vs fsync
package main
import (
"log"
"math/rand"
"os"
"syscall"
"testing"
"time"
@komuw
komuw / panic_handler.go
Last active December 16, 2020 16:58
global panic handler in Go
package main
import "fmt"
// Usage:
//
// func main() {
// defer panicHandler()
// }
//
@komuw
komuw / go_errors_with_stacktrace.go
Created November 9, 2019 15:36
Golang errors with stack traces
package main
import (
"fmt"
"runtime"
"strconv"
"strings"
)
/*