Skip to content

Instantly share code, notes, and snippets.

do something
package serve
import (
"errors"
"io"
)
type pipedMessage struct {
buffer []byte
count int
@aryszka
aryszka / lru.go
Last active November 15, 2016 12:50
LRU cache example (non-concurrent)
package main
import "time"
type entry struct {
key string
data []byte
expiration time.Time
lessRecent, moreRecent *entry
}
@aryszka
aryszka / Makefile
Last active February 7, 2017 17:26
Reproducing EOF/write-pipe errors on Go HTTP client side, when server closes an idle connection
default: run
gencert:
go run /home/aryszka/go/src/crypto/tls/generate_cert.go --host localhost
cert.pem: gencert
key.pem: gencert
init: cert.pem key.pem
package main
import (
"time"
"math/rand"
"log"
circuit "github.com/rubyist/circuitbreaker"
)
package main
import "fmt"
type syncMap chan map[string]int
func newSyncMap() syncMap {
m := make(map[string]int)
sm := make(syncMap, 1)
sm <- m
@aryszka
aryszka / queue.go
Last active May 18, 2017 16:31
Go queue with a buffered channel
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
func produce(q chan<- int, n int) {
@aryszka
aryszka / rxstream.go
Created June 22, 2017 17:18
regexp replace in a stream, buffering only the necessary amount of bytes, but all if there is no match
/*
Try:
cat | go run rxstream.go '(?ms)(.*)42' '{"chars before 42": "$1"}'
And input:
abc42abc42
*/
package main
import (
"fmt"
"log"
"math/rand"
"os"
"time"
)
@aryszka
aryszka / csv-sort.mml
Last active August 15, 2017 22:54
sort csv in mml
#! /bin/mml
// sort csv by a column
import (
csvparser "mml/parser/csv"
csvtemplate "mml/template/csv"
"mml/function"
"mml/lists"
"mml/log"