This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Database server */ | |
package main | |
import ( | |
"fmt" | |
"html" | |
"log" | |
"net/http" | |
"net/url" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package di | |
import ( | |
"bytes" | |
"io" | |
"testing" | |
) | |
// Watcher takes an io.Writer as an attribute | |
type Watcher struct { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "log" | |
// An interface for an abstraction between structs | |
type StringWriter interface { | |
Write(string) | |
} | |
// LogWriter now implements StringWriter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "log" | |
// This violates Dependency Inversion Principle | |
type LogWriter struct { | |
Counter int | |
} | |
func (w *LogWriter) Write(msg string) { | |
log.Println(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() { |