Skip to content

Instantly share code, notes, and snippets.

package p2p
import (
"bytes"
"fmt"
"os"
"os/signal"
"runtime"
"sync"
"sync/atomic"
#!/bin/sh
set -e
PROJECTROOT=$GOPATH/src/github.com/<you>/<repo>
cd $PROJECTROOT
echo "Vetting project."
go vet || exit 1
echo "...ok"
@aybabtme
aybabtme / clever.sh
Created April 18, 2014 20:38
> To apply, email us at jobs+n@getclever.com, where n is the number of students in the Clever sandbox API data set with a last name that begins with the letter 'V'. Send us one or more things from [resume, github profile, recent side projects, linkedin URL, Stack Overflow profile, etc], and let us know how you solved our 'puzzle.'
pagecount=$(curl -s -H 'Authorization: Bearer DEMO_TOKEN' -X GET https://api.clever.com/v1.1/students | jq '.paging.total')
echo "$pagecount pages"
now=$(date "+%Y-%m-%d-%H:%M.%S")
tmpfile=".$now-tmp_clever.json"
touch $tmpfile
echo "Saving pages to $tmpfile"
for i in $(seq 1 $pagecount); do
echo "Getting page $i"
@aybabtme
aybabtme / errors.go
Last active August 29, 2015 14:01
Lazy error handling for overly verbose error producers.
// accumulates non-nil errors
type errors []error
func (e *errors) Ack(err error) {
if err != nil {
*e = append(*e, err)
}
}
// Example usage:
// Triggers an io.ErrUnexpectedEOF on the last read
func TestCanDiffWhenOldlistIsCorrupted(t *testing.T) {
log.SetOutput(testwriter(t)) // log to testing.Log
oldKeys := []s3.Key{{ETag: "1"}, {ETag: "3"}}
newKeys := []s3.Key{{ETag: "1"}, {ETag: "2"}, {ETag: "3"}}
want := []s3.Key{{ETag: "2"}}
oldList := encodeKeys(oldKeys)
@aybabtme
aybabtme / logger.go
Last active August 29, 2015 14:02
test logger
// io.Writer implementer
type writer func(p []byte) (int, error)
func (w writer) Write(p []byte) (int, error) { return w(p) }
// magic, a testing.T writer!
func testwriter(t *testing.T) io.Writer {
return writer(func(p []byte) (int, error) {
t.Log(string(p))
return 0, nil
switch {
case olderr != nil && newerr != nil:
return diff, fmt.Errorf("reading both source, %v", olderr)
case olderr != nil:
return diff, fmt.Errorf("reading old source, %v", olderr)
case newerr != nil:
return diff, fmt.Errorf("reading new source, %v", newerr)
default:
return diff, nil
}
@aybabtme
aybabtme / syslog_formatter.go
Created December 26, 2014 20:16
syslog formatter middleware for Logrus
package log
import (
"fmt"
"log/syslog"
"os"
"github.com/Sirupsen/logrus"
)
// LogHook builds a func that can be fired by a doge/log through
// log.AddHook. If the entry it fires on contains an error, it will
// include a stacktrace of the logger's callsite.
//
// import "doge/log"
//
// log.AddHook(
// sentry.LogHook("my.pkg/name"),
// log.Lerror, log.Lpanic, log.Lfatal,
@aybabtme
aybabtme / gist:6bf2edacedabc7445c38
Created February 4, 2015 06:05
Most useful GoSublime snippet.
"snippets": [
{
"text": "sortby",
"title": "type bySomeOrder []type",
"value": "type $1 []$2\n\nfunc(b $1) Len() int { return len(b) }\nfunc(b $1) Swap(i, j int) { b[i],b[j] = b[j],b[i] }\nfunc(b $1) Less(i, j int) bool { return b[i].$3 < b[j].$3 }"
},
],