Skip to content

Instantly share code, notes, and snippets.

@kylelemons
kylelemons / extraarghandler.go
Created June 22, 2011 17:07
How to pass "extra arguments" to handlers in Go
type HandlerData struct {
data string
//whatever
}
func main(){
//Create a data struct
// add data to struct
h := &HandlerData{...}
@kylelemons
kylelemons / httpget.go
Created July 23, 2011 01:40
Send a bunch of HTTP requests via threads and goroutines
package main
import (
"fmt"
"http"
"flag"
"runtime"
"bytes"
"log"
)
@kylelemons
kylelemons / Makefile
Created October 20, 2011 18:27
A Makefile for protocol buffers in Go
include $(GOROOT)/src/Make.inc
PROTOFILES=$(wildcard *.proto)
GOPROTOFILES=$(patsubst %.proto,%.pb.go,$(PROTOFILES))
TARG=protopackage
GOFILES=\
$(GOPROTOFILES)\
CLEANFILES+=$(GOPROTOFILES)
@kylelemons
kylelemons / client.go
Created August 9, 2011 20:49 — forked from zhujo01/client.go
TLS echo server and client
package main
import (
"crypto/tls"
"io"
"log"
)
func main() {
conn, err := tls.Dial("tcp", "127.0.0.1:8000", nil)
@kylelemons
kylelemons / piping.go
Last active July 4, 2022 01:25 — forked from dagoof/piping.go
piping exec.Cmd in golang (example sorts all regular files under a directory by their extension)
package main
import (
"bytes"
"exec"
"log"
"os"
)
// Pipeline strings together the given exec.Cmd commands in a similar fashion
@kylelemons
kylelemons / k_from_p.go
Last active April 3, 2022 04:43
Maximum value of K coins taken from P piles in Go, using Dynamic Programming
// Leetcode:
// https://leetcode.com/problems/maximum-value-of-k-coins-from-piles/
func maxValueOfCoins(piles [][]int, k int) int {
// Record the points cumulatively for taking N coins from each pile
for p := range piles {
for c := range piles[p] {
if c+1 >= len(piles[p]) {
continue
}
@kylelemons
kylelemons / regexpswitch.go
Created August 18, 2011 16:34
Switch on a regex pattern
package main
import "fmt"
import "regexp"
var email = regexp.MustCompile(`^[^@]+@[^@.]+\.[^@.]+$`)
var shortPhone = regexp.MustCompile(`^[0-9][0-9][0-9][.\-]?[0-9][0-9][0-9][0-9]$`)
var longPhone = regexp.MustCompile(`^[(]?[0-9][0-9][0-9][). \-]*[0-9][0-9][0-9][.\-]?[0-9][0-9][0-9][0-9]$`)
func main() {
@kylelemons
kylelemons / routes.java
Created September 6, 2017 20:30
VpnService.Builder code for excluding local ranges
// IPv4
//
// Excluded ranges:
// 10.0.0.0/8
// 172.16.0.0/12
// 192.168.0.0/16
// 100.64.0.0/10
// 224.0.0.0/4
// 240.0.0.0/4
builder.addRoute("0.0.0.0", 5)
@kylelemons
kylelemons / io_fs_write_sketch.go
Last active May 9, 2021 00:04
Sketch of writable filesystem interfaces for io/fs
package fs
import (
"fmt"
"io"
"os"
. "io/fs"
)
@kylelemons
kylelemons / linked_hash_map.go
Created February 25, 2021 06:01
Linked Hash Map in Generic Go (go2go)
// https://go2goplay.golang.org/p/MGkmOhAcV79
package main
import (
"fmt"
)
type Pair[K, V any] struct {
Key K