Skip to content

Instantly share code, notes, and snippets.

Title: Waiting for goroutines to finish
URL slug: waitgroup
Description: A WaitGroup provides a simple way to wait for a collection of goroutines to perform a task.
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
)
@eliben
eliben / thoughts.rst
Last active October 12, 2018 04:53
Thoughts on the Go 2 Error Handling proposal

TL;DR: The proposal looks good, except the handler chaining part, which adds a lot of magical complexity to cater to rare use cases. Chaining can always be added at a later stage without breaking backwards compatibility, if its lack is deemed unbearable.

As I see it, the biggest issues in current Go usage the proposal tackles are:

  1. Repeated sequences of if err != nil {return nil, err} littering Go code
  2. Lack of proper context in propagated errors (see (1) above)

To fix these issues, I believe the proposed check keyword with a single handler per function are sufficient. Using stacks of handlers for proper cleanup is best left to defer, which is already a familiar tool. Thus, handle should only exist as a default "report more context in case of an error and return the error" mechanism. Where a single handler appears insufficient because the nature of error handling required changes throug

@eliben
eliben / templatelexer.py
Created August 8, 2012 09:33
initial attempt implementing the go template lexer in Python
from collections import namedtuple
TOK_TEXT = 'TOK_TEXT'
TOK_LEFT_META = 'TOK_LEFT_META'
TOK_RIGHT_META = 'TOK_RIGHT_META'
TOK_DUMMY = 'TOK_DUMMY'
# A token has
@eliben
eliben / Loop benchmark
Last active December 30, 2015 03:39
C benchmark demonstrating bizarre performance behavior of Intel CPUs
const unsigned N = 400 * 1000 * 1000;
volatile unsigned long long counter = 0;
// Don't inline the benchmarking code into main
void __attribute__((noinline)) tightloop();
void __attribute__((noinline)) loop_with_extra_call();
void tightloop() {
unsigned j;
@eliben
eliben / gist:6202493
Last active December 20, 2015 22:09
import io
from test.support import import_fresh_module
import csv
csv_other = import_fresh_module('csv', fresh=['_csv', 'csv'])
f = io.StringIO('foo\x00,bar\nbaz,42')
reader = csv.reader(f)
====> lli
lli
AsmParser
Core
Support
Support
BitReader
Core
Support
IRReader
struct SS {
long n1;
long n2;
long n3;
long n4;
};
int foo(struct SS s) {
@eliben
eliben / gist:3314190
Created August 10, 2012 13:23
ROLE_REGEX for my Python plugin article
# Regex for matching/capturing role text.
# E.g. :name:`text` - first capture group is "name", second group is "text"
#
ROLE_REGEX = re.compile(r':(\w+):`([^`]*)`')
@eliben
eliben / gist:2944722
Created June 17, 2012 14:42
Timer utility
class Timer(object):
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
if self.name: