<?xml version="1.0" encoding="iso-8859-1"?>
        <?xml-stylesheet type="text/css" href="http://www.miek.nl/blog/"?>
<rss version="2.0"
 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:dc="http://purl.org/dc/elements/1.1/"
 xmlns:admin="http://webns.net/mvcb/"
 xmlns:atom="http://www.w3.org/2005/Atom"
>
<channel>
<title>Filed under: programming | Miek</title>
<atom:link href="http://www.miek.nl/blog/archives/programming/index-rss.xml" rel="self" type="application/rss+xml" />
<link>http://www.miek.nl/blog</link>
<description>Thoughts on (technical) stuff</description>
<dc:language>en-us</dc:language>
<dc:creator>Miek Gieben</dc:creator>
<dc:date>2012-02-04T04:15:11+01:00</dc:date>
<admin:generatorAgent rdf:resource="http://nanoblogger.sourceforge.net" />

<item>
<link>http://www.miek.nl/blog/archives/2011/12/24/convert_vim_colors_to_gvim_colors/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2011/12/24/convert_vim_colors_to_gvim_colors/index.html</guid>
<title>Convert vim colors to gvim colors</title>
<dc:date>2011-12-24T00:08:12+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> linux, programming</dc:subject>
<description><![CDATA[<p>I've tweaked my vim color scheme
<a href="http://www.miek.nl/blog/archives/2011/09/11/xoria256m_color_scheme/index.html">quite a bit</a>
and tried
to keep the colors of gvim (which I use less often) in sync.</p>

<p>This keeping in sync hasn't worked out, so I wrote this little
script to convert the vim colors to the gvim ones:</p>

<p>Download the <a href="http://www.miek.nl/downloads/2011/makegvim">makegvim script</a>, and
use it like:</p>

<pre><code>$ ./makegvim &lt; ~/.vim/colors/&lt;yourfile&gt; &gt; /tmp/x
$ mv /tmp/x ~/.vim/colors/&lt;yourfile&gt;
</code></pre>

<p>And now the colors of <code>gvim</code> should be identical to those of <code>vim</code>.</p>

<p>Note: colors like <code>000</code> must be used like <code>cterm=0</code> otherwise
the conversion fails (the script isn't that smart).</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2011/12/13/cherry-picking_remote_branches/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2011/12/13/cherry-picking_remote_branches/index.html</guid>
<title>Cherry-picking remote branches</title>
<dc:date>2011-12-13T08:51:14+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> linux, programming</dc:subject>
<description><![CDATA[<p>I've create a little tool (actually an XSLT file) that
helps to write RFCs. Browsing my <a href="https://github.com/miekg/pandoc2rfc">github repo</a>
I found two different forks. And browsing those
forks, I saw some commits I wanted to have.</p>

<p>But how do you merge a commit from a forked git repository?
Turns out it is not that difficult.</p>

<p>The commit I want has 
the hash <a href="https://github.com/hamnis/pandoc2rfc/commit/5a11e88ddbef4ce7513aae93bdcd377449f45efb">5a11e88ddbef4ce7513aae93bdcd377449f45efb</a>.</p>

<p>The steps:</p>

<ul>
<li><p>Create a remote branch:</p>

<pre><code>git remote add hamnis https://github.com/hamnis/pandoc2rfc
</code></pre></li>
<li><p>Fetch the contents of it:</p>

<pre><code>git fetch hamnis
</code></pre></li>
<li><p>Cherry-pick the commit you need:</p>

<pre><code>git cherry-pick -c 5a11e88ddbef4ce7513aae93bdcd377449f45efb
[master 7501f13] transform.xsl: Supporting the output from pandoc 1.8.2.1:  
* table/title 
instead of table/caption  
* table/tgroup/thead/row/entry instead of table/thead/tr/th  
* table/tgroup/tbody/row/entry instead of table/tbody/tr/td
 Author: Trygve Laugstøl &lt;trygvis@inamo.no&gt;
 1 files changed, 3 insertions(+), 3 deletions(-)
</code></pre></li>
</ul>

<p>Voila. </p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2011/09/28/pandoc_to_rfc/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2011/09/28/pandoc_to_rfc/index.html</guid>
<title>Pandoc to RFC</title>
<dc:date>2011-09-28T13:45:18+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> programming</dc:subject>
<description><![CDATA[<p>This is an follow-up on <a href="http://www.sidnlabs.nl/laatste-berichten/nieuwsdetail/article/van-pandoc-naar-rfc/">this <code>pandoc</code> item in Dutch</a>.</p>

<p>When writing <a href="http://www.ietf.org/rfc/rfc4641.txt">RFC 4641</a> we directly wrote the
XML. Needless to say is was kinda tedious even thought the XML of <a href="http://xml.resource.org/">xml2rfc</a>
is very "light".</p>

<p>Nowadays I'm a fan of the markdown syntax and especially the syntax as supported (created?)
by <a href="http://johnmacfarlane.net/pandoc/">Pandoc</a>.</p>

<p>So for my next RFC (if ever!) I decided I wanted to use Pandoc. As <code>xml2rfc</code> uses XML
I thought the easiest way would be to create <code>docbook</code> XML and transform that using
XSLT.</p>

<p>The XML generated is suitable for inclusion in either the <code>middle</code> or <code>back</code> section
of an RFC. The easiest way is to create a template xml file and include the appropriate
XML:</p>

<pre><code>&lt;?xml version='1.0' ?&gt;
&lt;!DOCTYPE rfc SYSTEM 'rfc2629.dtd'&gt;

&lt;rfc ipr='trust200902' docName='draft-gieben-pandoc-writing-rfcs-01'&gt;
 &lt;front&gt;
    &lt;title&gt;Writing I-Ds and RFCs using Pandoc&lt;/title&gt;
&lt;/front&gt;

&lt;middle&gt;
    &lt;?rfc include="middle.xml"?&gt;
&lt;/middle&gt;

&lt;back&gt;
    &lt;?rfc include="back.xml"?&gt;
&lt;/back&gt;

&lt;/rfc&gt;
</code></pre>

<p>See the Makefile for an example of this. In this case you need to edit
3 documents:</p>

<ol>
<li>middle.mdk;</li>
<li>back.mkd;</li>
<li>template.xml (probably a fairly static file).</li>
</ol>

<p>The draft (<code>draft.txt</code>) is automatically created when you call <code>make</code>. 
Note that this <code>README.mkd</code> is converted to an RFC-like document when you call <code>make</code>.</p>

<p>It needs <code>xsltproc</code> and <code>pandoc</code> to be installed.</p>

<p>What is supported?</p>

<ul>
<li>section with anchor and title attributes, see <a href="http://johnmacfarlane.net/pandoc/README.html#headers">README.html</a>;</li>
<li>list style=symbols, see <a href="http://johnmacfarlane.net/pandoc/README.html#lists">README.html</a>;</li>
<li>list style=numbers;</li>
<li>list style=letters;</li>
<li>list style=format, both lowercase and uppercase roman numerals;</li>
<li>list style=hanging, see <a href="http://johnmacfarlane.net/pandoc/README.html#definition-lists">README.html</a>;</li>
<li>figure/artwork, see <a href="http://johnmacfarlane.net/pandoc/README.html#indented-code-blocks">README.html</a>;</li>
<li>blockquote - this isn't supported by <code>xml2rfc</code>, so this is converted to
figure/artwork paragraph. See <a href="http://johnmacfarlane.net/pandoc/README.html#block-quotations">README.html</a>;</li>
<li>refs: external (eref), see <a href="http://johnmacfarlane.net/pandoc/README.html#inline-links">README.html</a>;</li>
<li>refs: internal (xref) see <a href="http://johnmacfarlane.net/pandoc/README.html#header-identifiers-in-html">README.html</a>;</li>
<li>spanx style=verb, style=emph, style=strong, see <a href="http://johnmacfarlane.net/pandoc/README.html#inline-formatting">README.html</a>;</li>
<li>tables, see <a href="http://johnmacfarlane.net/pandoc/README.html#tables">README.html</a>.</li>
</ul>

<p>What's not?</p>

<ul>
<li>list style=empty;</li>
<li>irefs: index (no pandoc syntax available);</li>
<li>crefs: for comments (there is no pandoc syntax for this).</li>
</ul>

<p>The heavy lifting is done by <code>transform.xsl</code> that transforms the Docbook XML to xml2rfc XML.</p>

<p><a href="https://github.com/miekg/pandoc2rfc">Check out the pandoc2rfc github repository</a>.</p>

<h1>Pandoc Constructs</h1>

<p>What do you need to type in Pandoc to get the correct output. Note this
is just basic Pandoc format, so you might be better off reading the
<a href="http://johnmacfarlane.net/pandoc/README.html">README from Pandoc itself</a>.</p>

<h2>section</h2>

<p>Just use the normal sectioning commands available in Pandoc, I tend to use</p>

<pre><code># Section1 One
Bla
</code></pre>

<p>Converts to xml2rfc: <code>&lt;section title="Section1 One" anchor="section1-one"&gt;</code>
If you have another section that is also named "Section1 One", that
anchor will be called "section1-one-1". Referencing the section
is done with <code>See [](#section1-one)</code>, see <a href="#refs-internal"></a>.</p>

<h2>list style symbols</h2>

<pre><code>A symbol list.

* Item one;
* Item two.
</code></pre>

<p>Converts to xml2rfc: <code>&lt;list style="symbol"&gt;</code></p>

<h2>list style numbers</h2>

<pre><code>A numbered list.

1. Item one;
2. Item two.
</code></pre>

<p>Converts to xml2rfc: <code>&lt;list style="numbers"&gt;</code></p>

<h2>list style letters</h2>

<pre><code>A numbered list.

a. Item one;
b. Item two.
</code></pre>

<p>Converts to xml2rfc: <code>&lt;list style="letters"&gt;</code></p>

<h2>list style format</h2>

<pre><code>A roman numbered list.

I.  Item one;
II.  Item two.
</code></pre>

<p>(note: 2 spaces after the numerals).</p>

<p>Converts to xml2rfc: <code>&lt;list style="format %I."&gt;</code></p>

<h2>list style hanging</h2>

<p>This is more like a description list, so we need to use:</p>

<pre><code>First item that needs clarification

:   Explanation one
More stuff, because item is difficult to explain.
* item1
* item2

Second item that needs clarification

:   Explanation two
</code></pre>

<p>Converts to xml2rfc: <code>&lt;list style="hanging"&gt;</code> and <code>&lt;t hangText="First item..."&gt;</code></p>

<h2>figure/artwork</h2>

<p>Just indent the paragraph with 4 spaces.</p>

<p>Converts to xml2rfc: <code>&lt;figure&gt;&lt;artwork&gt; ...</code></p>

<h2>blockquote</h2>

<p>This is not supported by xml2rfc, but any paragraph like:</p>

<pre><code>&gt; quoted text
</code></pre>

<p>Converts to xml2rfc: <code>&lt;figure&gt;&lt;artwork&gt; ...</code></p>

<h2>refs: external</h2>

<p>Any reference like:</p>

<pre><code>[Click here](URI)
</code></pre>

<p>Converts to xml2rfc: <code>&lt;ulink target="URI"&gt;Click here ...</code></p>

<h2>refs: internal</h2>

<p>Any reference like:</p>

<pre><code>[Click here](#localid)
</code></pre>

<p>Converts to xml2rfc: <code>&lt;link target="localid"&gt;Click here ...</code> </p>

<p>For referring to RFCs (for which you manually need add the reference source in the template), 
you can just use:</p>

<pre><code>[](#RFC2119)
</code></pre>

<p>And it does the right thing. Referencing sections is done with:</p>

<pre><code>See [](#pandoc-constructs)
</code></pre>

<p>The word 'Section' is inserted automatically: ... See <a href="#pandoc-constructs"></a> ...</p>

<h2>spanx style</h2>

<p>The verb style can be selected with back-tics:</p>

<pre><code>`text`
</code></pre>

<p>Converts to xml2rfc: <code>&lt;spanx style="verb"&gt; ...</code></p>

<p>And the emphasis style with asterisks:</p>

<pre><code>*text*
</code></pre>

<p>Converts to xml2rfc: <code>&lt;spanx style="emph"&gt; ...</code></p>

<p>And the emphasis style with double asterisks:</p>

<pre><code>**text**
</code></pre>

<p>Converts to xml2rfc: <code>&lt;spanx style="strong"&gt; ...</code></p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2011/09/11/xoria256m_color_scheme/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2011/09/11/xoria256m_color_scheme/index.html</guid>
<title>Xoria256m color scheme</title>
<dc:date>2011-09-11T15:59:27+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> linux, programming</dc:subject>
<description><![CDATA[<p>I recently came across <a href="http://ethanschoonover.com/solarized">solarized</a>. 
I started to use it immediately
for vim and mutt, but after a few days the low contrast of the color scheme
started to annoy me. Oh and btw, I'm red/green color blind.</p>

<p>I went searching and found "xoria256" a color scheme suited for 256 color
terminal and a dark background. There is even a Ubuntu/Debian package
for it: <code>vim-scripts</code>. Unlike solarized it doesn't come with a custom palette, just
use Tango in gnome-terminal (or whatever your favorite is).</p>

<p>But of course I wasn't completely happy with it as I wanted some more
blue-ish colors in there.</p>

<p>So tweaked it a little, and this is the result:</p>

<p><a href="http://miek.nl/gfx/2011/vim-xoria256.png"><img width="400px"
src="http://miek.nl/gfx/2011/vim-xoria256.png" title="Xoria256m with VIM" /></a></p>

<p>I've also ported it over to mutt, so that it has a matching color theme.</p>

<ul>
<li><a href="http://miek.nl/downloads/2011/xoria256.vim">Download the vim color scheme</a>;</li>
<li><a href="http://miek.nl/downloads/2011/color-xoria">Download mutt color scheme</a>.</li>
</ul>

<p>Note: I haven't changed the name of the color scheme, but it should be
called something different, xoria256m or something. It is also still a
work-in-progress, I will probably open a git repository on github soonish.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2011/07/18/on_programming_languages_and_programmers/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2011/07/18/on_programming_languages_and_programmers/index.html</guid>
<title>On programming languages and programmers</title>
<dc:date>2011-07-18T13:02:23+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> go, programming</dc:subject>
<description><![CDATA[<p>Very well written email message from Geoff Teale
on the <a href="http://groups.google.com/group/golang-nuts/">golang mailing list</a> on programmers and
progamming languages
(<a href="http://groups.google.com/group/golang-nuts/browse_thread/thread/350bf027bd803af1#">thread</a>).</p>

<blockquote>
  <p>To summarise a long presentation I gave to non-programmers:</p>

<ul>
<li>There are 12 million programmers in the world</li>
<li>The majority of those programmers are scarcely qualified</li>
<li>Most technology decisions are made by a combination of following the crowd
and a false understanding of risk.</li>
<li>The high cost and failure rate in software development is no coincidence.</li>
</ul>

<p>Remember the Stevie Wonder rule - "When you believe in something you don't
understand then you suffer".   In this case that means "Perhaps making
programming language decisions based on what 12 million powerless idiots are
doing isn't the golden road to glory and great hacks."</p>

<p>Go is a genuine attempt to improve the state of systems programing language
beyond the point they reached in the early 1970s.  As a result the sort of
people using it are mostly that small community of people who understand and
care about the concerns that drive such a development.</p>

<p>You're not going to catch those 12 million people unless you can market heavily
enough the idea that their future income depend on jobs/contacts built around
go, but that goal just draws resources and energy away from making the language
better.</p>

<p>Arguably Java also suffers from it's large community of corporate drones.   The
slavish tendency to build baroque, mausoleums of intricate classes, dense with
state and dripping with verbose XML is a reflection of the unthinking insanity
of the 12 million.</p>

<p>I'd rather a tiny community use the language well, built successful
applications and organically grew the user base whilst establishing a clean,
sane library base that might later be used to improve the lives of a wider
population of programmers.</p>

<p>I've said more than enough, I'll trundle back to the twelve million and take my
punishment now.</p>
</blockquote>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2010/08/01/computer_languages/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2010/08/01/computer_languages/index.html</guid>
<title>Computer Languages </title>
<dc:date>2010-08-01T19:17:00+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> programming</dc:subject>
<description><![CDATA[<p>I have done programming in (or at least looked at) the following computer
languages during my live. Of course the world is not a perfect place,
but some languages out there are just plain awful.</p>

<ul>
<li><p>BASIC - with line numbers!
At the time (I was 11) didn't know there was something else
out there. Fun and easy language, although I never programmed
in it ever again.</p></li>
<li><p>Pascal -
After BASIC, there is no going back to Pascal. Only briefly
looked at it in my youth and discarded it in favor for BASIC...</p></li>
<li><p>PHP -  a language for children, by children.
Insecure, weird and no namespaces (and that for a dynamic language).
I always need to look up function prototypes, like the difference of
the thing you are looking for in these functions:</p></li>
</ul>

<p><code><pre>
    int preg_match ( string $pattern , string $subject ...
</pre></code></p>

<p>And</p>

<p><code><pre>
    int substr_compare ( string $main_str , string $str ...
</pre></code></p>

<p>And then you find a nice function on <a href="http://www.php.net">www.php.net</a>, which 
  is 'deprecated' because of various reasons.</p>

<ul>
<li><p>C - old, but functional.
Obviously no gc and namespaces, but you get a real sense of power 
and nicely documented in manual pages 
I still use C today for <a href="http://www.miek.nl/projects/rdup">rdup</a> for
instance.</p></li>
<li><p>Perl - love it.
Easy to write, easy to read (after using it for a few years).</p></li>
<li><p>Python - not for me.
I just don't like OO programming and the format rules are really not for me.</p></li>
<li><p>Unix Shell - sucks, except for short stuff.
See <a href="http://fvue.nl/wiki/Bash:_Piped_`while-read'_loop_starts_subshell">Bash: Gotchaes</a>:</p></li>
</ul>

<p><code><pre>
!/bin/bash
echo nothing | while read line; do
    foo=bar
    echo foo1: $foo
done
echo foo2: $foo
</pre></code></p>

<p>Example output:
<code><pre>
foo1: bar
foo2:  (shouldn't be empty)
</pre></code></p>

<ul>
<li><p>Java - never dared venturing in to it.
Way too verbose and again I don't really like OO programming.</p></li>
<li><p>C++ - I will rather be shot in the shoulder than to use this.
See this nice <a href="http://miek.nl/downloads/2010/c++-talk.pdf">pdf</a> for
some horrific examples, <code>Pdf</code> is
from <a href="http://www.fefe.de/c++/c++-talk.pdf">fefe.de</a> (Felix von
Leitner).</p></li>
<li><p>Lisp - nice language.
One of the oldest languages out there and still interesting. But
learning (common) Lisp now (without concurrency and stuff) isn't 
worth the effort.</p></li>
<li><p>Erlang - great language.
I could not cope with the syntax, although I even
found the functional aspect of it rather nice.</p></li>
<li><p>Go - better than C, simpler than C++.
And has concurrency, namespaces and gc. The more I code in it
the more I like it. Even the OO implementation (using Interfaces{}) is
nice.</p></li>
<li><p>Assorted macro languages, like LaTex and m4.
Fun, but obscure. </p></li>
</ul>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/11/24/prime_sieve_in_go_ofcourse/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/11/24/prime_sieve_in_go_ofcourse/index.html</guid>
<title>Prime sieve (in Go ofcourse)</title>
<dc:date>2009-11-24T22:55:58+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> programming</dc:subject>
<description><![CDATA[<p>After reading up on the 
<a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">prime sieve</a>, and
playing with Go for the past week I thought needed to implement this
algorithm in Go and make it parallel.</p>

<p>I want to create a set (<em>n</em> in <em>2</em>..<em>N</em>) 
<a href="http://golang.org/doc/go_lang_faq.html#goroutines">goroutines</a>.
Each of these routines will check if it can divide a number (<em>i</em>) by
<em>n</em> (integer division). If so the number <em>i</em> is not prime, otherwise
it is given to the next goroutine. Communication between the goroutines
is done via channels as in 
<a href="http://www.miek.nl/blog/archives/2009/11/16/first_steps_with_go/index.html">this example</a>.</p>

<h1>Sieve</h1>

<p>In this small example I have 5 goroutines, and we give it the
number <em>3</em> and <em>4</em>. The table shows the reminder when dividing
by <em>n</em>.</p>

<pre><code>            n = 2   3   4   5  ... ...
-------------------------------------------
i  = 3 % n      1   0   3   3   3   3   ...
i  = 4 % n      0   1   0   4   4   3   ...
</code></pre>

<p>From here we see that <em>i = 3</em> is prime, because it has
an <em>1</em> in the column where <em>n = 2</em>, <em>i = 4</em> is not prime, because
it has a zero in the column where <em>n = 2</em>.</p>

<p>The algorithm for each goroutine thus becomes (in pseudo code):</p>

<pre><code>function(n, i) {
if i == 0 then copy to next goroutine; return
if n &gt;= i then copy to next goroutine; return

// test for primeness, if not prime return the *magic* 
// number 0
if i % n == 0 then
    // not prime
    give 0 to the next goroutine
fi

give i to the next goroutine
}
</code></pre>

<p>Or in Go's syntax:</p>

<pre><code>func f(left, right chan int, n int) {
    i := &lt;-right;
    if i == 0 || n &gt;= i {
        left &lt;- i;
        return;
    }
    if i%n == 0 {
        // too bad
        left &lt;- 0;
        return;
    }
    left &lt;- i;
}
</code></pre>

<h1>Main</h1>

<p>The rest of the Go program should setup the goroutines, channels
and handle the flags. <em>N - 1</em> goroutines are set up, and each is
connected to its neighbor.</p>

<blockquote>
  <p>Even nicer would be to really do this in parallel, i.e. send 
<em>i</em> to all goroutines, and then just see if one of them returned
zero. If none of them do, its a prime. For now the following must
suffice.</p>
</blockquote>

<p>In Go code:</p>

<pre><code>leftmost := make(chan int);
var left, right chan int = nil, leftmost;
for n := 0; n &lt; *prime-1; n++ {
    left, right = right, make(chan int);
    go f(left, right, *prime-n);
}
right &lt;- *prime;        // bang!
x := &lt;-leftmost;        // wait for completion
fmt.Println(x);
</code></pre>

<p>This outputs 0 when the number tested is <em>not</em> prime, it outputs
the number itself given when it is prime.</p>

<pre><code>% ./sieve -p 32
0           # not prime

% ./sieve -p 11
11          # prime ;-)
</code></pre>

<h1>Complete listing</h1>

<pre><code>package main

import (
    "flag";
    "fmt";
    "os";
)

var prime = flag.Int("p", 13, "test for primeness")

func f(left, right chan int, n int) {
    i := &lt;-right;
    if i == 0 || n &gt;= i { 
        left &lt;- i;
        return;
    }   
    if i%n == 0 { 
        // too bad
        left &lt;- 0;
        return;
    }   
    left &lt;- i;
}

func main() {
    flag.Parse();

    if *prime &lt; 2 { 
        fmt.Fprint(os.Stderr, "You know this already\n");
        os.Exit(1);
    }   

    leftmost := make(chan int);
    var left, right chan int = nil, leftmost;
    for n := 0; n &lt; *prime-1; n++ {
        left, right = right, make(chan int);
        go f(left, right, *prime-n);
    }   
    right &lt;- *prime;        // bang!
    x := &lt;-leftmost;        // wait for completion
    fmt.Println(x);
}
</code></pre>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/11/23/more_go/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/11/23/more_go/index.html</guid>
<title>More Go</title>
<dc:date>2009-11-23T22:45:48+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> programming</dc:subject>
<description><![CDATA[<p>Still learning and playing with <a href="http://www.golang.org">Go</a>, I've
rewritten my 
<a href="http://www.miek.nl/blog/archives/2009/11/18/cat_in_go/index.html">cat experiment</a>, to
use a <code>*bufio.Reader</code>, which is more correct I think. I'm also slowly
wrapping my mind around the concept of
<a href="http://golang.org/doc/go_spec.html#Interface_types">Interfaces</a>. As a
non-OO programmer (C and non-OO Perl) is starting to see why this is
useful.</p>

<p>So today's exercises:</p>

<ol>
<li>Write a cat implementation in Go. See below.</li>
<li>Write a grep implementation in Go. See below. </li>
</ol>

<h1>Cat in go</h1>

<pre><code>package main

// A implementation of cat in Go

import (
    "os";
    "fmt";
    "bufio";
    "flag";
)

var numberFlag = flag.Bool("n", false, "number each line")

func cat(r *bufio.Reader) bool {
    i := 1;
    for {
        buf, e := r.ReadBytes('\n');
        if e == os.EOF {
            break
        }
        if *numberFlag {
            fmt.Fprintf(os.Stdout, "%5d  %s", i, buf);
            i++
        } else {
            fmt.Fprintf(os.Stdout, "%s", buf)
        }
    }
    return true;
}

func main() {
    flag.Parse();
    if flag.NArg() == 0 {
        cat(bufio.NewReader(os.Stdin))
    }
    for i := 0; i &lt; flag.NArg(); i++ {
        f, e := os.Open(flag.Arg(i), os.O_RDONLY, 0);
        if e != nil {
            fmt.Fprintf(os.Stderr, "%s: error reading from %s: %s\n",
                os.Args[0], flag.Arg(i), e.String());
            continue;
        }
        if !cat(bufio.NewReader(f)) {
            os.Exit(1)
        }
    }
}
</code></pre>

<h1>Grep in go</h1>

<pre><code>package main

// A implementation of Unix grep in Go
// TODO(mg) better error handling

import (
    "os";
    "fmt";
    "bufio";
    "regexp";
    "flag";
)

var numberFlag = flag.Bool("n", false, "number each line")
var filenameFlag = flag.Bool("l", false, "print names of matching files")

func grep(r *bufio.Reader, reg string) (match bool) {
    i := 0;
    for {
        buf, e := r.ReadBytes('\n');
        i++;
        if e == os.EOF {
            break
        }
        if m, _ := regexp.Match(reg, buf); m == true {
            match = true;
            if *filenameFlag {
                return match
            }
            if *numberFlag {
                fmt.Fprintf(os.Stdout, "%5.d:  %s", i, buf)
            } else {
                fmt.Fprintf(os.Stdout, "%s", buf)
            }
        }
    }
    return match;
}

func main() {
    flag.Parse();
    if flag.NArg() &lt; 1 {
        fmt.Fprintf(os.Stderr, "%s: missing regexp\n", os.Args[0]);
        os.Exit(1);
    }
    if flag.NArg() == 1 {
        if grep(bufio.NewReader(os.Stdin), flag.Arg(0)) {
            if *filenameFlag {
                fmt.Fprintf(os.Stdout, "(standard input)\n");
            }
        }
    }
    for i := 1; i &lt; flag.NArg(); i++ {
        f, e := os.Open(flag.Arg(i), os.O_RDONLY, 0);
        if e != nil {
            fmt.Fprintf(os.Stderr, "%s: error reading from %s: %s\n",
                os.Args[0], flag.Arg(i), e.String());
            continue;
        }
        if grep(bufio.NewReader(f), flag.Arg(0)) {
            if *filenameFlag {
                fmt.Fprintf(os.Stdout, "%s\n", flag.Arg(i))
            }
        }
    }
}
</code></pre>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/11/18/cat_in_go/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/11/18/cat_in_go/index.html</guid>
<title>cat in Go</title>
<dc:date>2009-11-18T19:57:50+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> programming</dc:subject>
<description><![CDATA[<p>After spending every free minute to <code>Go</code> I'm starting to get a feel
for the language. Every one has to start somewhere, so I decided to 
"port" Unix utils to Go. I'm starting with <code>cat</code>, and thanks to 
the Go tutorial this is the result.</p>

<pre><code>package main

// An implementation of Unix cat in Go

import (
    "os";
    "fmt";
    "flag";
)

func cat(filename string) bool {
    const NBUF = 512;
    var buf [NBUF]byte;
    if f, e := os.Open(filename, os.O_RDONLY, 0); e != nil {
        fmt.Fprintf(os.Stderr, "cat: error reading from %s: %s\n",
            filename, e.String());
        return true;
    } else {
        for {
            switch nr, _ := f.Read(&amp;buf); true {
            case nr &lt; 0:
                os.Exit(1)
            case nr == 0:   // EOF
                return true
            case nr &gt; 0:
                if nw, ew := os.Stdout.Write(buf[0:nr]); nw != nr {
                    fmt.Fprintf(os.Stderr,
                        "cat: error writing from %s: %s\n",
                        filename, ew.String());
                    return false;
                }
            }
        }
    }
    return true;
}

func main() {
    flag.Parse();   // implement -n TODO
    if flag.NArg() == 0 {
        cat("/dev/stdin")
    }
    for i := 0; i &lt; flag.NArg(); i++ {
        if !cat(flag.Arg(i)) {
            os.Exit(1)
        }
    }
}
</code></pre>

<p>In something like 50 lines you have a <code>cat</code> program. I'm really starting 
to like Go. Next up: extra features and a <code>grep</code> command.</p>]]></description>

</item>
<item>
<link>http://www.miek.nl/blog/archives/2009/11/16/first_steps_with_go/index.html</link>
<guid isPermaLink="true">http://www.miek.nl/blog/archives/2009/11/16/first_steps_with_go/index.html</guid>
<title>First steps with Go</title>
<dc:date>2009-11-16T22:43:04+01:00</dc:date>
<dc:creator>Miek Gieben</dc:creator>
<dc:subject> programming</dc:subject>
<description><![CDATA[<p>I joined the go-nuts mailing list a few days ago and it really feels
good to receive 200+ emails per day again. Just like in the good old
days before good spam filtering (and anti-spam laws).</p>

<p>I also re-watched the presentation Rob Pike gave for Google Tech Talks
on youtube.com. In there he presented the following program <code>chain.go</code>:
(Formatted with <code>gofmt</code> as it should)</p>

<pre><code>package main

import (
    "flag";
    "fmt";
)

var ngoroutine = flag.Int("n", 100000, "how may")

func f(left, right chan int)    { left &lt;- 1+&lt;-right }

func main() {
    flag.Parse();
    leftmost := make(chan int);

    var left, right chan int = nil, leftmost;
    for i := 0; i &lt; *ngoroutine; i++ {
        left, right = right, make(chan int);
        go f(left, right);
    }
    right &lt;- 0;             // bang!
    x := &lt;-leftmost;        // wait for completion
    fmt.Println(x);         // 100000
}
</code></pre>

<p>In this short program we make a chain of 100000 <em>goroutines</em> which are
connected to each other. Each one adds 1 to the value it gets from its
right neighbor. We start it of by giving the last one (<em>right</em>) a value
of 0. Then we wait until they are finished and print it. </p>

<h2>Compile and run</h2>

<p>To compile the above program you</p>

<pre><code>8g chain.go 
8l -o chain chain.8
</code></pre>

<p>And then run it</p>

<pre><code>./chain
</code></pre>

<p>On my crappy laptop this takes about 2.3 seconds. Not too bad :)</p>

<h2>Vim</h2>

<p>For Go code editing, I've added the following to my <code>~/.vimrc</code></p>

<pre><code>autocmd Filetype go set textwidth=0
autocmd Filetype go set noexpandtab
autocmd Filetype go set tabstop=8
autocmd Filetype go set shiftwidth=8
autocmd Filetype go set softtabstop=8
autocmd Filetype go set number
autocmd Filetype go command! Fmt %!gofmt
</code></pre>

<p>The last line adds a new command which reformats your Go code. Just
do <code>&lt;ESC&gt;:Fmt</code> and you'll end up with properly formatted Go code.
The coding style rules for Go are simple. Its what you get when you
run it through to <code>gofmt</code>.</p>]]></description>

</item>
</channel>
</rss>

