Skip to content

Instantly share code, notes, and snippets.

package main
import (
"fmt"
)
type myInt int
func (i myInt) square() myInt {
return i*i
func CriticalDataManager(cmdchan chan CriticalCommand) {
queued := new(vector.Vector)
critical := 0
for {
var rawcmd CriticalCommand
if critical == 0 && queued.Len() > 0 {
// Process the queued-up commands when we aren't in critical section
rawcmd := queued.At(0)
queued.Delete(0)
} else {
==> ./sub/ctpkg.c <==
#include <stdio.h>
void PrintIt() {
printf("PASS\n");
}
==> ./sub/cgotpkg.go <==
package tpkg
@kylelemons
kylelemons / funcall.go
Created June 6, 2011 15:43
Benchmark function calls
package main
import (
"fmt"
"testing"
)
func readonly() {}
var readwrite = func() {}
@kylelemons
kylelemons / reman.go
Created June 7, 2011 00:44
A sketch of a resource manager in Go
package main
import (
"fmt"
"time"
"sync"
)
// A Token represents resources allocated. It must be given
// back to the Manager to free the resource.
@kylelemons
kylelemons / workers.go
Created June 7, 2011 01:44
Multiple workers in go
wg := new(sync.WaitGroup)
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case input, open := <-In:
if !open { return }
@kylelemons
kylelemons / rfc1035header.go
Created June 9, 2011 03:38
decode rfc1035 header
type Header struct {
raw [6*2]byte
Opcode, Rcode, Z byte
ID, QD, NS, AN, AR []byte // 2 bytes each
QR, AA, TC, RD, RA bool
}
func decodeHeader(r io.Reader) *Header, os.Error {
h := &Header{}
n, err := r.Read(h.raw[:])
@kylelemons
kylelemons / mdsha.go
Created June 10, 2011 21:47
command-line utility for SHA1 hashing - modified from Guillermo Estrada
/*
Hash - Guillermo Estrada
(modified to demonstrate Go idioms by Kyle Lemons)
Simple utility to obtain the MD5 and/or SHA-1
of a file from the command line.
2011
*/
@kylelemons
kylelemons / neuronbcast.go
Created June 20, 2011 20:27
How to broadcast a message to peers
type neuron struct {
coef float64
input chan *stimulus
output []chan *stimulus
}
func newNeuron(coef float64) *neuron {
n := &neuron{
coef: coef,
input: make(chan *stimulus),
@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{...}