Skip to content

Instantly share code, notes, and snippets.

View arschles's full-sized avatar
🎯
Focusing

Aaron Schlesinger arschles

🎯
Focusing
View GitHub Profile
package main
import "github.com/gorilla/mux"
func main() {
r := mux.NewRouter()
r.HandleFunc("/reservations/{key}", func1).Methods("POST")
r.HandleFunc("/values/{key}/{lock_id}", func2).Queries("release", "").Methods("POST")
r.HandleFunc("/values/{key}", func3).Methods("PUT")
var (
ErrInvalidCommand = errors.New("invalid command")
ErrInvalidNumArgs = errors.New("wrong num args")
ErrInvalidInt = errors.New("invalid int")
)
const (
create = "CREATE"
modify = "MODIFY"
set = "SET"
@arschles
arschles / readline.go
Created July 15, 2015 16:21
golang read a file line by line
import (
"bufio"
"os"
)
func main() {
f := os.Open("somefile")
defer f.Close
rdr := bufio.NewReader(f)
for {
@arschles
arschles / handlerwrap.go
Last active August 29, 2015 14:23
go handler wrapping
func myHandlerFunc(i int) http.HanderFunc {
return func(w http.ResponseWriter, r *http.Request) {
str := fmt.Sprintf("hello %d", i)
w.Write([]byte(str))
}
}
@arschles
arschles / tmpl_bench.go
Last active October 11, 2015 05:48
golang html/template Parse benchmark
package tmplbench
import (
"testing"
"html/template"
)
func BenchmarkTemplate(b *testing.B) {
tmpl := template.New("test")
b.ResetTimer()
@arschles
arschles / single-consumer-mailbox.scala
Created April 18, 2014 00:28
single consumer mailbox example
import akka.actor.{Props, Actor, ActorSystem}
class A extends Actor {
override def receive = {
case other => println(other)
}
}
object Main extends App {
val system = ActorSystem("main")
package goactor
import "container/list"
import "sync"
//public types
type Message struct {
Sender Pid
Payload interface{}
}
@arschles
arschles / scalaConcurrentPatterns.scala
Last active August 23, 2018 15:00
examples of common concurrency patterns that you can achieve with the scala.concurrent package
import scala.concurrent._
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.Random
import java.util.{Timer, TimerTask}
object Util {
sealed trait BaseResponse
case class Response1(res: Int) extends BaseResponse
case class Response2(res: String) extends BaseResponse
//Newman has a DSL for building up HttpRequests.
//the DSL uses the familiar builder pattern from Java.
//it copies the state of the request on each call, instead of modifying any internal state.
val url = new URL("http://siliconvalley-codecamp.com")
//the familiar GET function returns an instance of a Builder
//you can convert any Builder to a Newman HttpRequest
val builder1 = GET(url)
val httpClient: HttpClient = ???
val httpResponse: HttpResponse = Await.result(httpClient.get(someUrl, someHeaders).apply(), 1.second)
//bodyAsIfResponseCode will return a Success if:
//- the HttpResponse returned with HttpResponseCode.OK
//- the function in the second argument returned a Success
val bodyValidation1: Validation[Throwable, String] = httpResponse.bodyAsIfResponseCode(HttpResponseCode.OK,
{ httpResponse: HttpResponse =>
Success(httpResponse.bodyString)
}