Skip to content

Instantly share code, notes, and snippets.

View matryer's full-sized avatar
🔨
Building things at @grafana

Mat Ryer matryer

🔨
Building things at @grafana
View GitHub Profile
@matryer
matryer / gopher.json
Last active August 1, 2017 13:32
Gopherize.me Gopher JSON endpoint
{
"id": "8d784442c57fa1647259a37c277c6738b2d79d6a",
"images": [
"artwork/010-Body/blue_gopher.png",
"artwork/020-Eyes/crazy_eyes.png",
"artwork/024-Glasses/all_black_sunglasses.png",
"artwork/027-Extras/laptop.png"
],
"original_url": "https://storage.googleapis.com/gopherizeme.appspot.com/gophers/8d784442c57fa1647259a37c277c6738b2d79d6a.png",
"url": "https://lh3.googleusercontent.com/dnKffPVPSYSIXhelVR8q88IM2tHxTQ8GgxlCezQjjqLrGvQI9roQIF71NfaOYN5m3txAfbpzyA48nklQMTZ5wgvG",
@matryer
matryer / artwork.go
Created August 1, 2017 12:41
Gopherize.me's artwork API response structure
type ArtworkResponse struct {
Categories []Category `json:"categories"`
TotalCombinations int `json:"total_combinations"`
}
type Category struct {
ID string `json:"id"`
Name string `json:"name"`
Images []Image `json:"images"`
}
@matryer
matryer / gopherize.me.artwork.json
Created August 1, 2017 12:36
Response of Artwork API for Gopherize.me
{
"categories": [{
"id": "artwork/010-Body",
"name": "Body",
"images": [{
"id": "artwork/010-Body/blue_gopher.png",
"name": "blue gopher",
"href": "https://storage.googleapis.com/gopherizeme.appspot.com/artwork/010-Body/blue_gopher.png",
"thumbnail_href": "https://lh3.googleusercontent.com/2WSrI5x6u8LqWQ-xBzogIu3QrthPiawLH2zCPvrOgpMPU0K240di8pOKSwhz_IL0xiq5I2pBJkWrhyZDJeAPWD4=s71"
}, {
@matryer
matryer / handle_greeter.go
Created August 1, 2017 12:15
Functions that return handlers let you neatly set things up
func handleGreeter() http.Handler {
// TODO: do any setup stuff here
//... like prepare a template:
tpl, err := template.ParseFiles("pages/_layout.html", "pages/gopher.html")
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if err != nil {
// parsing the template failed?
@matryer
matryer / sending_mock.go
Created July 11, 2017 21:25
Moq generated mock of a simple interface
package sending
// AUTOGENERATED BY MOQ - DO NOT EDIT
// github.com/matryer/moq
import (
"sync"
)
var (
@matryer
matryer / term_context.go
Last active March 10, 2022 05:23
Making Ctrl+C termination cancel the context.Context
func main() {
ctx := context.Background()
// trap Ctrl+C and call cancel on the context
ctx, cancel := context.WithCancel(ctx)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
defer func() {
signal.Stop(c)
@matryer
matryer / speaker.go
Created May 21, 2017 15:49
Speaks the names of people detected from facebox (Machine Box)
package main
import (
"encoding/json"
"log"
"os"
"os/exec"
)
type result struct {
### Keybase proof
I hereby claim:
* I am matryer on github.
* I am matryer (https://keybase.io/matryer) on keybase.
* I have a public key ASCC8YrsmU_t9rWCOgUqUGE0Zv9wb519A25FyiDH7xMXfAo
To claim this, I am signing this object:
@matryer
matryer / params_test.go
Created August 28, 2016 13:52
Simple path parameter parsing function tests
func TestPathParams(t *testing.T) {
r, err := http.NewRequest("GET", "1/2/3/4/5", nil)
if err != nil {
t.Errorf("NewRequest: %s", err)
}
params := pathParams(r, "one/two/three/four")
if len(params) != 4 {
t.Errorf("expected 4 params but got %d: %v", len(params), params)
}
for k, v := range map[string]string{
@matryer
matryer / params.go
Created August 28, 2016 13:44
Simple path parameter parsing function for Go
// pathParams parses the URL.Path of the Request with the given
// pattern, and extracts the value for each segment into a map.
func pathParams(r *http.Request, pattern string) map[string]string {
params := map[string]string{}
pathSegs := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
for i, seg := range strings.Split(strings.Trim(pattern, "/"), "/") {
if i > len(pathSegs)-1 {
return params
}
params[seg] = pathSegs[i]