Skip to content

Instantly share code, notes, and snippets.

@scottcagno
scottcagno / bm.go
Created August 8, 2016 19:31
bitmap hack on playhround
package main
import "fmt"
var (
tA = [8]byte{1, 2, 4, 8, 16, 32, 64, 128}
tB = [8]byte{254, 253, 251, 247, 239, 223, 191, 127}
)
func main() {
@scottcagno
scottcagno / httpscraper.go
Created August 31, 2016 17:41
HTTP Scraper by URL in Golang (Not tested, just wrote in here)
type mu sync.RWMutex
func ScrapeFromURL(ptrn []byte, urls ...string) [][]byte {
mu.Lock()
defer mu.Unlock()
var data [][]byte
for _, url := range urls {
res, err := http.Get(url)
if err != nil {
log.Fatal(err)
@scottcagno
scottcagno / load-balancer.go
Created September 20, 2016 17:18
General purpose sketch of a load balancer in Golang
package main
// http://playground.gregpechiro.com/py_-fzzLle
import (
"container/heap"
"fmt"
"math/rand"
"time"
)
@scottcagno
scottcagno / db-notes.txt
Last active October 12, 2016 19:45
Notes
string/[]byte casting:
The []byte(s) is not a cast but a conversion. Some conversions are the
same as a cast, like uint(myIntvar), which just reinterprets the bits
in place. Unfortunately that's not the case of string to byte slice
conversion. Byte slices are mutable, strings (string values to be precise)
are not. The outcome is a necessary copy (mem alloc + content transfer)
of the string being made. So yes, it can be costly in some scenarios. *No
encoding transformation is performed. The string (source) bytes are copied
to the slice (destination) bytes as they are.
@scottcagno
scottcagno / collect.go
Created January 5, 2017 20:30
Go Collection Functions
// Returns a new slice containing all strings in the slice that satisfy the predicate f.
func Filter(vs []string, f func(string) bool) []string {
vsf := make([]string, 0)
for _, v := range vs {
if f(v) {
vsf = append(vsf, v)
}
}
return vsf
}
package main
import (
"fmt"
"strings"
)
type Doc struct {
Off int
Len int
const SLAB int64 = (1 << 22) // 4MB
type Doc struct {
Off int
Len int
}
type Engine struct {
File *os.File
Docs map[string]*Doc
@scottcagno
scottcagno / balance.go
Created April 20, 2017 18:03
golang load balancer primitive start
package main
import (
"container/heap"
"flag"
"fmt"
"math/rand"
"time"
)
@scottcagno
scottcagno / protected.go
Created April 27, 2017 18:18
protected decorator
func Protected(next http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
c, err := r.Cookie("sid")
if err != nil || c.Value == "" {
http.Redirect(w, r, r.Referer(), 301)
return
}
var ausr AuthUser
err := db.Return("session", c.Value, &ausr)
if err != nil {
@scottcagno
scottcagno / hashmap.go
Created May 2, 2017 23:46
golang hashmap implementation
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package runtime
// This file contains the implementation of Go's map type.
//
// A map is just a hash table. The data is arranged
// into an array of buckets. Each bucket contains up to