Skip to content

Instantly share code, notes, and snippets.

View natdm's full-sized avatar

Nathan Hyland natdm

View GitHub Profile
definition(
name: "Smarter Nightlight",
author: "Nathan Hyland",
description: "Turns on lights when it's dark and motion is detected and lights are not already on. Turns lights off when it becomes light or some time after motion ceases.",
category: "Convenience",
iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-luminance.png",
iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_motion-outlet-luminance@2x.png"
)
preferences {
@natdm
natdm / PIPELINES.md
Last active November 3, 2017 18:22
Showing composable, concurrent pipelines in Go

Pipelines in Go are really neat. They're composable functions that allow you to chain concurrent events together, safely.

If you have the need to call API after API after API, witl the same data, maybe pipelines could help. Imagine you have the following:

func create(item Object) Object {
  // do things, call APIs
  return item
}
@natdm
natdm / gscript.sh
Last active January 17, 2017 16:50
AWS AMI script for Go/Glide/Git
#!/bin/bash
# Install and extract go
sudo wget -P /home/ec2-user/ https://storage.googleapis.com/golang/go1.7.4.linux-amd64.tar.gz
cd /home/ec2-user/
tar -xzf go1.7.4.linux-amd64.tar.gz
# Set up env. variables
export GOROOT=/home/ec2-user/go
export PATH=$PATH:$GOROOT/bin
@natdm
natdm / readByes.md
Created September 26, 2016 16:15
Compare bytes from a slice of bytes to a file and replace them with other bytes if found.

Reading and replacing some bytes!

Here is the entire application. Below that is a step by step guide.

package main

import (
	"bytes"
	"io/ioutil"
@natdm
natdm / app.go
Created September 26, 2016 15:40
Check if a file has a particular set of bytes in it, and replace it with other bytes.
package main
import (
"bytes"
"io/ioutil"
"log"
"os"
)
//Open a file, read the bytes, check if the bytes have a sequence of bytes, then replace those bytes with other ones
@natdm
natdm / app.go
Created September 22, 2016 14:26
Playing with context
package main
import (
"fmt"
"time"
"golang.org/x/net/context"
)
func main() {
@natdm
natdm / main.go
Created September 22, 2016 02:11
Testing Negroni middleware knowledge
package main
//Messing with Negroni subroutes.
import (
"fmt"
"io"
"log"
"net/http"
"os"
@natdm
natdm / slice.go
Created September 21, 2016 05:17
Messing with golang arrays and slices, for kicks.
package main
import (
"fmt"
)
func main() {
//initialize an array with len/cap 6
a := [6]string{"G", "o", "l", "a", "n", "g"}
@natdm
natdm / main.go
Created September 13, 2016 00:28
Playing with file concurrency
package main
/*
This isn't meant to be the idiomatic way to read files and copy them - this is a learning exercise
for myself. I wanted to be able to read multiple chunks of a file and write them concurrently to a
new file
I don't really handle all errors with log.Fatalln. It's just done here in the interest of keeping the focus on concurrency.
Example output:
@natdm
natdm / goroutines.go
Created July 20, 2016 14:00
Explaining how to make DB calls with goroutines and waitgroups.
//GetTransactionsAndLinesForBidder hits the database 3 times. Let's make that fast.
func (db *DB) GetTransactionsAndLinesForBidder(bidderID int) ([]BidderTransaction, error) {
//Establish a waitgroup. This has 3 functions only. Add, Wait, and Done. You add to the WaitGroup and hit Done when you are
// done with a process. It will stop the function at Wait until the WaitGroup is at 0.
var wg sync.WaitGroup
//Get the transaction 'shell'
trans, err := db.GetTransactionsForBidder(bidderID)
if err != nil {