Skip to content

Instantly share code, notes, and snippets.

@kylelemons
kylelemons / jsonwrap.go
Created April 17, 2012 17:58
Decoding JSON within a wrapper using interface{}
package main
import (
"encoding/json"
"fmt"
"io"
"strings"
)
type ResponseError struct {
@kylelemons
kylelemons / client.go
Created April 4, 2012 19:59
Server and client RPC benchmarks
package main
import (
"net/rpc"
"fmt"
"sync"
. "testing"
)
package main
func main() {
var (
in = [3]int{3, 9, 5}
out = [3]int{}
i = 0
)
for i, out[i] = range in {
@kylelemons
kylelemons / wikitemplate.go
Created March 4, 2012 07:23
How to safely parse wiki text and insert links with html/template.
package main
import (
"bytes"
"fmt"
"html/template"
"log"
"regexp"
)
@kylelemons
kylelemons / infinite.go
Created January 12, 2012 01:27
Infinite channel buffer without "sync"
// assume the following exist and are set to unbuffered channels:
// var (
// next chan Type
// in chan Type
// )
go func() {
defer close(next)
// pending events (this is the "infinite" part)
@kylelemons
kylelemons / humanize.go
Created January 11, 2012 23:15
Humanize a date! (go r60)
package main
import "fmt"
import "time"
func Humanize(t *time.Time) string {
const (
Minute = 60
Hour = 60 * Minute
Day = 24 * Hour
@kylelemons
kylelemons / piping.go
Last active May 31, 2024 09:07 — forked from dagoof/piping.go
piping exec.Cmd in golang (example sorts all regular files under a directory by their extension)
package main
import (
"bytes"
"exec"
"log"
"os"
)
// Pipeline strings together the given exec.Cmd commands in a similar fashion
@kylelemons
kylelemons / graph.go
Created November 17, 2011 19:01
A fun little graph demo
package main
import (
"fmt"
"sync"
)
type WaitList []*sync.WaitGroup
func (l WaitList) Done() {
for _, wg := range l {
@kylelemons
kylelemons / gencert.sh
Created November 17, 2011 02:55
Generate a key/certificate
#!/bin/bash
# Usage: gencert.sh
# Generates SSL certificates
set -e
PREFIX="my_"
function oops() {
echo "Failed."
exit 1
@kylelemons
kylelemons / killproxy.go
Created November 16, 2011 01:52
A potential way to kill all connections in a proxy server
package main
import (
"http"
"net"
"os"
"url"
)
var reverse = http.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: "google.com"})