Skip to content

Instantly share code, notes, and snippets.

@jochasinga
jochasinga / server.go
Created November 9, 2016 20:37
Database server in Go that uses url.Value as temporary key-value store and path parameter + query string to set and get data.
/* Database server */
package main
import (
"fmt"
"html"
"log"
"net/http"
"net/url"
@jochasinga
jochasinga / printTime.ino
Created October 3, 2016 09:15
Get and print local time from an Arduino Yun.
#include <Process.h>
Process date; // process used to get the date
int dates, month, years, hours, minutes, seconds; // for the results
int lastSecond = -1; // need an impossible value for comparison
void setup() {
Bridge.begin(); // initialize Bridge
Serial.begin(9600); // initialize serial
@jochasinga
jochasinga / di_test.go
Last active June 1, 2016 03:21
Dependency injection in unit testing
package di
import (
"bytes"
"io"
"testing"
)
// Watcher takes an io.Writer as an attribute
type Watcher struct {
@jochasinga
jochasinga / di_3.go
Last active May 31, 2016 14:33
Example of dependency injection in action
package di
import (
"fmt"
"regexp"
)
// This is a pretty dumb function. It can only works
// if you want to stay with "Hello" forever.
func PrintIfMatchedHello(msg string) {
package main
import "log"
// An interface for an abstraction between structs
type StringWriter interface {
Write(string)
}
// LogWriter now implements StringWriter
@jochasinga
jochasinga / di_1.go
Last active May 31, 2016 12:30
Sample structs which violate Dependency Inversion Principle
package main
import "log"
// This violates Dependency Inversion Principle
type LogWriter struct {
Counter int
}
func (w *LogWriter) Write(msg string) {
log.Println(msg)
@jochasinga
jochasinga / counting_sort.go
Created May 26, 2016 15:45
Counting sort implementation in Go
package main
import "fmt"
func countingSort(numbers []int, maxValue int) []int {
// Allocate a numCounts slice with cap of the maxValue + 1
// Make sure it's allocated (zero-filled) and not empty.
numCounts := make([]int, maxValue+1)
@jochasinga
jochasinga / fibonacci_closure.go
Created May 26, 2016 12:57 — forked from tetsuok/fibonacci_closure.go
An answer of the exercise: Fibonacci closure on a tour of Go
package main
import "fmt"
// Very naive answer.
// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
n := 0
a := 0
@jochasinga
jochasinga / switcher_example.go
Last active April 5, 2016 16:40
Using Switcher in Go Relay package
// Error handling omitted for brevity
var (
helloMarsHandler = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello Mars client!")
}
goodDayHandler = func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Good day client!")
}
palomaHandler = func(w http.ResponseWriter, r *http.Request) {
@jochasinga
jochasinga / testing_relay.go
Last active April 5, 2016 16:37
Example of using Relay Proxy to simulate latent connection in testing
// Error handling omitted for brevity
func TestGet(t *testing.T) {
Convey("GIVEN the test server", t, func() {
ts := httptest.NewServer(http.HandlerFunc(handler))
Convey("GIVEN a slow connection which takes 4s to-from the server", func() {
// this delay is per trip, meaning it doubles for a round trip
delay := time.Duration(2) * time.Second
conn := relay.NewProxy(delay, ts)
Convey("WITH a client that times out after 3s", func() {