Skip to content

Instantly share code, notes, and snippets.

@kylelemons
kylelemons / bcast.go
Created September 20, 2011 16:55
The WRONG way to do broadcast
package main
import (
"sync"
"fmt"
)
// DO NOT DO THIS.
func main() {
l := new(sync.Mutex)
@kylelemons
kylelemons / valsort.go
Created September 22, 2011 21:36
Sort a map by its values
package main
import "fmt"
import "sort"
func main() {
m := map[string]int{
"One": 1,
"Two": 2,
"Three": 3,
@kylelemons
kylelemons / gotour-48.go
Created October 6, 2011 22:59
A solution to Go Tour #48
package main
import (
"fmt"
"math/cmplx"
)
func Cbrt(x complex128) (z complex128) {
// Initial guess is x
z = x
@kylelemons
kylelemons / gotour-69.go
Created October 7, 2011 01:55
My solution to Go Tour #69 - Web Crawler
package main
import (
"os"
"fmt"
)
type Fetcher interface {
// Fetch returns the body of URL and
// a slice of URLs found on that page.
@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 / bitmap.go
Created October 22, 2011 18:33
Using strings to parse bitmaps
package main
import "fmt"
import "strconv"
func b(bin string) uint8 {
n, _ := strconv.Btoui64(bin, 2)
return uint8(n)
}
@kylelemons
kylelemons / enum.go
Created October 23, 2011 17:26
Enumerations in Go with string names
package main
import "fmt"
type day int
const (
Sunday day = iota
Monday
Tuesday
Wednesday
@kylelemons
kylelemons / redef.go
Created October 24, 2011 21:06
Redefine non-local method (should be compile error)
package main
import (
"bytes"
"fmt"
)
func (c *bytes.Buffer) String() string {
return "Whut?"
}
@kylelemons
kylelemons / dispatch.go
Created November 3, 2011 21:14
Using sscanf to dispatch strings to functions
package main
import "fmt"
func foo(i int, s string) {
fmt.Println("foo", i, s)
}
func bar(s string) {
fmt.Println("bar", s)
$ rm VERSION ../VERSION
rm: cannot remove `VERSION': No such file or directory
rm: cannot remove `../VERSION': No such file or directory
$ hg id
d7322ae4d055+ weekly.2011-09-21
$ ./version.bash
weekly.2011-11-09 9839+
$ hg tags | grep weekly | sed 's/:.*//'
weekly.2011-11-09 10370
weekly 10370