Skip to content

Instantly share code, notes, and snippets.

@elazarl
elazarl / BlockReadWriter.go
Created May 30, 2011
golang - A blocking reader
View BlockReadWriter.go
package main
import (
"fmt"
"bytes"
"os"
"log"
"time"
"io"
"http"
@elazarl
elazarl / redcon.go
Created Jun 6, 2011
redirect network stream
View redcon.go
package main
import (
"log"
"io"
"os"
"crypto/tls"
"strings"
"net"
"flag"
View goroutine2coroutine.py
#!/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 Dec 20, 2011
Trying to recreate the "Holder" class publishing error from JCIP.
View Publishing.java
/**
* 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 Jan 21, 2012
A simple and minimal C implementation of trie
View trie.c
#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 Feb 1, 2012
A simple bencode encoder that can handle streaming data
View BencodeWriter.java
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 Mar 16, 2012
Client.Do will not uncompress response body
View clientDoGzip.go
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 Mar 25, 2012
A draft code to read a partial response from http server
View partialResp.go
package main
import ("bufio"
"fmt"
"io"
"net/http"
"net"
"strings")
type readFirstCloseSecond struct {
View byam_lavan.go
package main
import (
"io"
"os"
"time"
)
func singLine(line string,w io.Writer) {
time.Sleep(time.Second)
@elazarl
elazarl / cp.go
Last active Jun 27, 2020
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`
View cp.go
package cp
import (
"io"
"os"
)
func cp(dst, src string) error {
s, err := os.Open(src)
if err != nil {