Skip to content

Instantly share code, notes, and snippets.

View brnstz's full-sized avatar

Brian Seitz brnstz

View GitHub Profile
import "bytes"
// "Quote" a string in backticks so it can be safely used in a dynamic
// query as a field or table name. The returned string will be
// surrounded by backticks, and any backticks inside the string will
// escaped (by another backtick).
//
// For example, this won't work:
// db.Query("SELECT * FROM ?", "mytable")
//
@brnstz
brnstz / binary_search.go
Last active August 29, 2015 13:55
Binary search using interfaces in Go
// https://github.com/brnstz/algo
package fund
// Create an interface, steal ideas from part of core sort package.
type SearchSlice interface {
// Return true if val is less than value stored at index i
Less(val interface{}, i int) bool
// Return true if val is equal to value at index i
Equals(val interface{}, i int) bool
@brnstz
brnstz / insertion_sort.go
Last active August 29, 2015 13:56
Insertion sort in Go, using sort.Interface
// https://github.com/brnstz/algo
package sorting
import (
// Import the core sort package to use its interface, which declares
// Len(), Less(), and Swap()
"sort"
)
func InsertionSort(a sort.Interface) {
@brnstz
brnstz / insertion_sort_test.go
Created February 10, 2014 06:20
Test insertion sort in Go.
// https://github.com/brnstz/algo
package sorting_test
import (
"algo/sorting"
"fmt"
"io"
"os"
"sort"
@brnstz
brnstz / merge_sort.go
Created February 10, 2014 06:37
Merge sort in Go
// https://github.com/brnstz/algo
package sorting
import (
"sort"
)
// Expand sort.Interface to include the ability to swap via aux
type Interface interface {
// Set self[i] = other[j]
@brnstz
brnstz / merge_sort_test.go
Last active August 29, 2015 13:56
Testing merge sort in Go
// https://github.com/brnstz/algo
package sorting_test
import (
"algo/sorting"
"fmt"
"io"
"os"
"testing"
@brnstz
brnstz / quick_sort.go
Created February 13, 2014 02:10
Quicksort in Go
package sorting
import (
"math/rand"
"sort"
"time"
)
// Run a simple implementation of Quicksort on an incoming
// sort.Interface value
@brnstz
brnstz / noredirect.go
Last active August 29, 2015 14:00
HTTP client in Go that doesn't follow redirects
package main
import (
"errors"
"fmt"
"net/http"
)
var noRedirect = errors.New("no redirect")
@brnstz
brnstz / go-coding.md
Last active August 29, 2015 14:12
Go coding tips
package main
import (
"flag"
"fmt"
"log"
"github.com/brnstz/routine/wikimg"
)