Skip to content

Instantly share code, notes, and snippets.

@kylelemons
kylelemons / enc.go
Created February 18, 2013 20:03
Simple RSA encryption/decryption. It uses the same public and private key, so it encrypts a message to itself. It can only encrypt short data (RSA is more for encrypting the key to the data than the data itself).
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/pem"
"flag"
"io/ioutil"
@kylelemons
kylelemons / formtpl.go
Created February 16, 2013 17:20
Quick and dirty example of HTML forms with validation dropping data into a struct. Note that the scanf should probably be replaced with a type switch, because it likes tokenized input.
package main
import (
"fmt"
"html/template"
"net/url"
"os"
"regexp"
)
package main
import (
"encoding/hex"
"fmt"
)
var i2b = []byte("0123456789bcdfghjklmnpqrstuvwxyz")
var b2i = func() []byte {
var ascii [256]byte
benchmark old ns/op new ns/op delta
BenchmarkWriteDefault 14287 3258 -77.20%
BenchmarkWriteShortList 13116 3516 -73.19%
BenchmarkWriteCompact 10669 2897 -72.85%
BenchmarkWriteDiffable 16807 3786 -77.47%
benchmark old MB/s new MB/s speedup
BenchmarkWriteDefault 7.98 34.99 4.38x
BenchmarkWriteShortList 5.95 22.18 3.73x
BenchmarkWriteCompact 4.12 15.19 3.69x
@kylelemons
kylelemons / euler24_brute.go
Created December 9, 2012 05:58
Project Euler #24 in Go - Answer: [2 7 8 3 9 1 5 4 6 0]
package main
import (
"fmt"
)
const Max = 10
const Index = 1000000
func main() {
@kylelemons
kylelemons / preprocess.go
Created July 3, 2012 17:21
I don't recommend it, but you can preprocess Go code.
/*
You can preprocess this using the following:
gcc -E -x c -P -C main.go
*/
/* abs.h */
#define ABS(N, T) func Abs##N(v T) T { \
; if v < 0 { \
; return -v \
; } \
@kylelemons
kylelemons / passwd.go
Created May 16, 2012 15:54
Very simple unicode-aware password checking
package main
import (
"fmt"
"unicode"
)
func validPassword(s string) error {
next:
for name, classes := range map[string][]*unicode.RangeTable{
@kylelemons
kylelemons / makedict.go
Created May 11, 2012 00:25 — forked from lamvak/makedict.go
particularly disturbing fragment of my new poliqarp binding (in progress)
func MakeDict(prefix string, strings []string) (dict *Dict, err error) {
image_file_size := 0
dict, err = nil, nil
for _, s := range strings {
// accomodate the string itself, length counter and zero-byte marker
// for the reason of backwards debility
image_file_size += len(s) + 5
}
image_file, err := os.Create(prefix + `.image`)
defer image_file.Close()
@kylelemons
kylelemons / broadcast.go
Created April 30, 2012 22:47
Untested method of registering and unregstering broadcast channels
func manager() {
chans := map[chan data]bool
active := sync.WaitGroup{}
for {
select {
case c := <-register:
chans[c] = true
case c := <-unregister:
active.Wait()
delete(chans, c)
@kylelemons
kylelemons / bdr_proto.proto
Created April 18, 2012 01:06 — forked from spikebike/bdr_proto.proto
example for protobuf, go, and the repeated protobuf entry
package bdr_proto;
message request {
message blob {
required string hash = 1; // the sha256 checksum of the blob
required int32 size = 2; // the size of the blob
}
repeated blob blobs = 1;
}