Monday, December 9, 2013

I have an internal API that returns io.ReadCloser and I wanted to wrap in a charset decoder. I found the excellent library, but it only provides an io.Reader.

However, I found that's it's quite easy to create a simple pass-through io.ReadCloser with anonymous structs withs anonymous fields:

  rc := struct {
  }{mahonia.NewDecoder("latin2").NewReader(r), r}

Thursday, November 28, 2013

Go templates and partials

I was looking around to see how people usually implement partial templates with the standard go templating library.

The usual pattern is to create a layout template which includes a content template, and then each partial template starts with

{{define "content"}}
blah blah

As described in this excellent post the Go template library uses the file base name as template name, so it's very tempting to just use a directive inside the partial template itself to force a particular name.

But I find this boilerplate in each partial very annoying, so I found this work around:

Wednesday, January 25, 2012

python winpdb automation

I wasted some time trying to automate the launching of winpdb without asking the user for the password; hoping that what I found out will help somebody:

Passing the password to winpdb with -p is unsupported on unix, because of security issues (the command line parameters can be seen by anyone). However there is an undocumented --rid parameter which takes an integer which is used to construct a file name which will contain the password (beware: no newline after the password). winpdb will delete this password file after reading it. You should create this file it with appropriate permissions.

Here's a bash haiku to show what I mean:

 RID=$RANDOM; echo -n 'test' >/home/marko/.rpdb2_settings/passwords/$RID; winpdb --rid $RID --attach  

Monday, June 6, 2011

golang interface is not nil even if 'containing' a nil pointer

Today I was struggling with a weird issue with "err != nil" during a refactoring of the go9p library. Here is a simplified situation:

This prints:

"Test *main.Test 0x0 dummy"

here 'err' is not nil, even if nil is actually returned.

The problem arised because go9p has an inner API returning some custom *Error, which is then 'casted' to a os.Error interface in the public API layer, so that it can implement the io.Reader,... interfaces. All code using this standard API was getting errors even if no error was returned.

From what I understood, it goes this way: *Test implements os.Error. *Test can be nil since it's a pointer.
os.Error is an interface value. Any interface value can also be nil.
When a value of type X is put* in a variable (or return value) declared as interface Y, then a new value is created holding X. In our case a value of type os.Error contains a *Test, both of which can be nil.

"if err != nil" checks whether 'err', the interface value, is nil, which is not.  "%T" fetches the runtime type of err, which skips (confusingly) the interface envelope and prints *main.Test.
"%p" also skips the interface value, probably because it's not really a pointer (or perhaps because it sounded more natural to follow what the underlying real value points to).
Lastly "%s" invokes the String() method, which is implemented for *Test; here "self != nil" is true because here we have the real underlying pointer. 

So, assuming I understood correctly what's happening, we can generalize this issue to:

This is because an interface value has to have a "zero" value, and it's nil. If you assign a pointer which implements that interface, that pointer can point somewhere or be nil, it doesn't matter, it's a valid pointer; and if you assign this pointer to the interface value, that interface value now has a value which is not nil.

I imagine that otherwise it would be impossible to have a zero value for the interfaces:

So, in order to fix this code you have to catch and "rethrow" the error:

This might seem a big issue, but usually this kind of code already checks for errors and returns so you just have to pay attention to not assigning to a previously declared interface value like os.Error, for example here "err" was inferred as os.Error in a previous ":=" assignement and then used to hold a *Error:

Tuesday, January 25, 2011

scala generics and eclipse autocompletion

The Scala 2.8.1 compiler doesn't create correctly the generics signature in some cases

Eclipse 3.6 is very strict about the signatures and it's autocompletion feature throws an exception when you work on a java project using scala code:

The issue is being fixed in both projects, but in the meantime I needed to work in eclipse with the akka library, and this issue is annoying.

I wrote a small tool which tries to fix the scala classes. Currently I tested it only on scala-library.jar, which contained the "orElse" method whose signature affected my setup. I don't know if it fixes all the problems, but I tried it on my project and eclipse  doesn't complain.

For now I only tried to put that jar in the eclipse IDE classpath, while keeping the original scala-library.jar in the executable classpath.

You can get the code at:

Thursday, January 13, 2011


A colleague of mine told me today that that his girlfriend woudln't believe that there was an optical illusion in the animated dancer image that you can find at:

I was just about to play with html5 canvas drawing, and so I wanted to try out to pilot the illusion with a small red ball which can rotate in both directions, following an elliptical path (an illusion of perspective) and appearing to disappear when going "behind" the dancer.

I don't know if it works for all, but for me this simple (and unprecise) animation, combined with the psycologial effect introduced by clicking on a "change direction" button, swaps the dancer's direction immediately in my head, or at least within one rotation.

Click here to se a demo

Source code on github

Monday, December 13, 2010

scala specs functional matchers

I was reading and wondered if new matchers could be defined more tersely than:

So here it is, with a couple of implicits:

The example shows how to promote a function to a matcher. Currently I didn't find how to define
a function literal with call-by-name semantics, but for simple matchers where a call-by-value semantic is acceptable this trick works fine.