Skip to content

Instantly share code, notes, and snippets.

@elazarl
elazarl / BlockReadWriter.go
Created May 30, 2011 13:57
golang - A blocking reader
package main
import (
"fmt"
"bytes"
"os"
"log"
"time"
"io"
"http"
@elazarl
elazarl / redcon.go
Created June 6, 2011 08:45
redirect network stream
package main
import (
"log"
"io"
"os"
"crypto/tls"
"strings"
"net"
"flag"
#!/usr/bin/env python
# coroutine equivalent of http://play.golang.org/p/lKBbS56f-n
def generate(s,even,odd):
for i,c in enumerate(s):
if i%2 == 0:
even.send(c)
else:
odd.send(c)
@elazarl
elazarl / Publishing.java
Created December 20, 2011 18:02
Trying to recreate the "Holder" class publishing error from JCIP.
/**
* Trying to recreate the "Holder" class publishing error from JCIP.
* See http://stackoverflow.com/a/6812936/55094 for the description of the problem.
*/
public class Publishing {
static class Holder {
int h = 0;
int dummy = 0;
public Holder(int h) {
for (long i=0;i<1000*1000;i++) {
@elazarl
elazarl / trie.c
Created January 21, 2012 23:21
A simple and minimal C implementation of trie
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
// weird exercise from a friend
// need to Implement this struct
typedef struct attrib_t {
struct attrib_t* bytes[256];
const char *data;
@elazarl
elazarl / BencodeWriter.java
Created February 1, 2012 17:00
A simple bencode encoder that can handle streaming data
class BencodeWriter {
static class SizedInputStream {
public InputStream stream;
public int size;
public SizedInputStream(InputStream _stream,int _size) {stream = _stream;size = _size;}
}
public static class BencodeProducer {
private OutputStream out;
public BencodeProducer(OutputStream o) {out = o;}
@elazarl
elazarl / clientDoGzip.go
Created March 16, 2012 15:01
Client.Do will not uncompress response body
package main
// run this file with `go run clientDoGzip.go, and then file tmp.out to verify body is gzipped
import "net/http"
import "io/ioutil"
import "bytes"
import "fmt"
import "log"
import "compress/gzip"
@elazarl
elazarl / partialResp.go
Created March 25, 2012 18:11
A draft code to read a partial response from http server
package main
import ("bufio"
"fmt"
"io"
"net/http"
"net"
"strings")
type readFirstCloseSecond struct {
package main
import (
"io"
"os"
"time"
)
func singLine(line string,w io.Writer) {
time.Sleep(time.Second)
@elazarl
elazarl / cp.go
Last active June 27, 2020 14:42
Unfortunately, searching for "golang copy file" gives subtly wrong code snippets (e.g. https://groups.google.com/d/msg/golang-nuts/JNyQxQLyf5o/kbGnTUK32TkJ that don't check close error code). This is an attempt to copy file content from `src` to `dst`
package cp
import (
"io"
"os"
)
func cp(dst, src string) error {
s, err := os.Open(src)
if err != nil {