Skip to content

Instantly share code, notes, and snippets.

@bprosnitz
bprosnitz / perfbench_test.go
Last active January 12, 2016 00:05
Go benchmarks
//Put this file in $GOPATH/perfbench/perfbench_test.go
//go test -bench . -v perfbench/...
//
//go version go1.5.1 linux/amd64
//
//noop: 0
//var x interface{} = <int8>: 1
//var x interface{} = <*int8>: 0
//var x interface{} = <[]int16>: 1
//var x interface{} = <typedInterface(ptr)>: 0
@bprosnitz
bprosnitz / linediff.go
Created December 28, 2015 20:52
Diff two lines
package main
import (
"fmt"
"math"
"os"
)
func main() {
if len(os.Args) != 3 {
@bprosnitz
bprosnitz / tee_writer.go
Created October 6, 2015 16:19
Tee Writer - Write to two writers
package teewriter
type TeeWriter struct {
w1 io.Writer
w2 io.Writer
}
func (tw *TeeWriter) Write(p []byte) (n int, err error) {
n1, err1 := tw.w1.Write(p)
n2, err2 := tw.w2.Write(p)
@bprosnitz
bprosnitz / dump.go
Last active October 2, 2015 21:54
dump objects / deep print
func dump(seen map[reflect.Value]bool, rv reflect.Value) string {
if rv.Kind() == reflect.Invalid {
return "nil"
}
if _, ok := seen[rv]; ok {
return "SEEN[" + rv.Type().Name() + "]"
}
seen[rv] = true
method := rv.MethodByName("String")
@bprosnitz
bprosnitz / indirection_test.go
Created September 17, 2015 17:34
Go method indirection benchmarks
package fs
// indirection techniques in go
//BenchmarkDirectFib-12 2000000000 0.13 ns/op
//BenchmarkSavePtr-12 30 34612496 ns/op
//BenchmarkUseBool-12 1000000000 0.53 ns/op
//BenchmarkIface-12 2000000000 0.16 ns/op
//ok fs 27.053s
import "testing"
@bprosnitz
bprosnitz / tee_copy.go
Created September 16, 2015 00:07
Tee Copy in Go
package debug
import (
"fmt"
"io"
)
func teeCopy(w1 io.Writer, w2 io.Writer, r io.Reader) (totalN int, err error) {
buf := make([]byte, 1024)
for {
@bprosnitz
bprosnitz / airport_data
Last active August 29, 2015 14:27
Find flight times to locations around the world from a specified location
1,"Goroka","Goroka","Papua New Guinea","GKA","AYGA",-6.081689,145.391881,5282,10,"U","Pacific/Port_Moresby"
2,"Madang","Madang","Papua New Guinea","MAG","AYMD",-5.207083,145.7887,20,10,"U","Pacific/Port_Moresby"
3,"Mount Hagen","Mount Hagen","Papua New Guinea","HGU","AYMH",-5.826789,144.295861,5388,10,"U","Pacific/Port_Moresby"
4,"Nadzab","Nadzab","Papua New Guinea","LAE","AYNZ",-6.569828,146.726242,239,10,"U","Pacific/Port_Moresby"
5,"Port Moresby Jacksons Intl","Port Moresby","Papua New Guinea","POM","AYPY",-9.443383,147.22005,146,10,"U","Pacific/Port_Moresby"
6,"Wewak Intl","Wewak","Papua New Guinea","WWK","AYWK",-3.583828,143.669186,19,10,"U","Pacific/Port_Moresby"
7,"Narsarsuaq","Narssarssuaq","Greenland","UAK","BGBW",61.160517,-45.425978,112,-3,"E","America/Godthab"
8,"Nuuk","Godthaab","Greenland","GOH","BGGH",64.190922,-51.678064,283,-3,"E","America/Godthab"
9,"Sondre Stromfjord","Sondrestrom","Greenland","SFJ","BGSF",67.016969,-50.689325,165,-3,"E","America/Godthab"
10,"Thule Air Base","Thule","Greenl
@bprosnitz
bprosnitz / parsemojo.go
Last active November 23, 2015 17:24
Parse mojo bytes and group by allocations
package main
import (
"encoding/binary"
"encoding/hex"
"flag"
"fmt"
"log"
)
@bprosnitz
bprosnitz / iframe_contents.html
Created May 6, 2015 19:21
iframe origin security: dispatchEvent vs postMessage
<script>
window.addEventListener('message', function(event) {
console.log('iframe: Got postmessage in origin ' + location.origin + ' from origin: ' + event.origin + '(' + event.data + ')');
});
window.addEventListener('dispatchToIframe', function() {
console.log('iframe: Got dispatchEvent() in origin ' + location.origin + '(from index.html)');
});
setTimeout(function() {
window.dispatchEvent(new CustomEvent('dispatchFromIframe'));
@bprosnitz
bprosnitz / quickerbrownfox.go
Last active August 29, 2015 14:19
Brute force shortest sentence with all the letters
package main
import (
"fmt"
"io/ioutil"
"unicode"
"strings"
)
const noRepeatFirstLetter = true