Skip to content

Instantly share code, notes, and snippets.

View cep21's full-sized avatar

Jack Lindamood cep21

View GitHub Profile
@cep21
cep21 / httptrace_example.go
Last active July 28, 2023 15:23
Example of http trace in Go 1.7
package main
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/http/httptrace"
"os"
)
@cep21
cep21 / client_test.go
Last active June 13, 2023 04:54
Stub out RoundTripper to test HTTP client
type roundTripFunc func (r *http.Request) (*http.Response, error)
func (s roundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) {
return s(r)
}
func TestSword(t *testing.T) {
var c Client
c.Client.Transport = roundTripFunc(func(r *http.Request) (*http.Response, error) {
assert.Equal(t, r.URL.Path, "/v1/item/sword")
@cep21
cep21 / ctx_middleware_chain.go
Last active July 25, 2022 10:17
Example of using context.Value() as a middleware chain
package goexperiments
import (
"context"
"net/http"
)
type HandlerMiddleware interface {
HandleHTTPC(ctx context.Context, rw http.ResponseWriter, req *http.Request, next http.Handler)
}
@cep21
cep21 / main_test.go
Created June 11, 2018 18:22
A -race failing test
package main
import (
"testing"
"sync"
)
func TestAppend(t *testing.T) {
x := make([]string, 0, 6)
@cep21
cep21 / main_test.go
Last active June 24, 2022 02:41
Example test
package main
import (
"sync"
"testing"
)
func TestAppend(t *testing.T) {
x := []string{"start"}

Keybase proof

I hereby claim:

  • I am cep21 on github.
  • I am cep21 (https://keybase.io/cep21) on keybase.
  • I have a public key ASAwNIEREtDh3M0e-0fmJBK5AlukNaSfZzfs3spchpbB2Qo

To claim this, I am signing this object:

@cep21
cep21 / abstract_example.go
Last active March 20, 2020 16:06
Example of interface wrapping erasure
package main
// Given an interface
type I interface {
Func()
}
// And another interface
type I2 interface {
Another()
@cep21
cep21 / ship.go
Last active May 28, 2019 17:53
Abstracted space ship
type Ship2 struct {
GameKillable
PhysicalObject
}
type PhysicalObject struct {
LocX float64
LocY float64
Size float64
}
@cep21
cep21 / space.go
Last active May 28, 2019 17:53
Original spaceship
type Ship struct {
Health int
LocX float64
LocY float64
Size float64
}
func (s *Ship) Heal(amnt int) {
if s.Health >= 0 {
s.Health += amnt
package goexperiments
import (
"context"
"net/http"
)
type HandlerMiddleware interface {
HandleHTTPC(ctx context.Context, rw http.ResponseWriter, req *http.Request, next http.Handler)
}