Skip to content

Instantly share code, notes, and snippets.

@scottcagno
scottcagno / byteslicer.go
Created January 8, 2016 20:56
Golang Slice Helper Functions
package byteslicer
func Copy(a []byte) []byte {
b := make([]byte, len(a))
copy(b, a)
return b
}
func Cut(a []byte, i, j int) []byte {
copy(a[i:], a[j:])
@scottcagno
scottcagno / bplus.go
Last active March 26, 2021 17:02
b+ tree, hand transposed from c
/*
* // Copyright (c) 2021. Scott Cagno. All rights reserved.
* // Use of this source code is governed by a BSD-style (clause 3)
* // license that can be found in the root of this project in the LICENSE file.
*/
package bptree
import (
"bytes"
@scottcagno
scottcagno / encodeuint.go
Last active February 5, 2016 11:43
Encode a uint32 into a byte slice, write it to disk, and read it from disk.
package main
import (
"encoding/binary"
"io/ioutil"
"log"
"fmt"
)
const (
@scottcagno
scottcagno / go-mmap.go
Created February 8, 2016 10:03
Golang mmap test...
package main
import (
"fmt"
"os"
"syscall"
"unsafe"
)
func main() {
@scottcagno
scottcagno / bitvec.go
Created March 13, 2016 23:17
Golang Bit-Vector/Array Implementation
package bitvec
import (
"log"
"go/build"
)
// this is the word size of an int(32)
// in bits. an int(32) requires a min-
// imum of 4 bytes, each of which are
@scottcagno
scottcagno / cmdtimeout.go
Created May 27, 2016 18:47
Golang exec.Command Timeout Wrapper
package main
import (
"bytes"
"fmt"
"os/exec"
"time"
)
func run(timeout int, command string, args ...string) string {
@scottcagno
scottcagno / iploc.sh
Created June 3, 2016 19:48
Locate city and state based on IP address provided as first argument.
#!/usr/bin/env bash
IP=$1
LOC=$(curl -s ipinfo.io/$IP | sed -r 's/[{}]|("[a-z]+":\ )|("|",)|//g') #| tac | sed '1,4d' | tac | sed '1,3d')
echo -e "\n $IP\n$LOC\n"
@scottcagno
scottcagno / encoder.go
Last active July 26, 2016 10:25
Encoder interface to allow for creating and implementing your own encoders/compressors on the fly
package main
import (
"bytes"
"compress/gzip"
"fmt"
"io"
)
var dat = []byte(`[
@scottcagno
scottcagno / site-maintenance.html
Created August 2, 2016 16:21
Site Maintenance HTML5 Placeholder
<!doctype html>
<html>
<head>
<title>Site Maintenance</title>
<meta charset="UTF-8">
</head>
<style>
body {
text-align: center;
padding: 100px;
@scottcagno
scottcagno / struct-fields.go
Created August 4, 2016 20:57
Get Struct Fields From Struct
package main
import (
"fmt"
"reflect"
)
type T struct {
Id int
Name string