Posts

Contributing Some Go to Rosetta Code

Image
In any craft the best results are usually achieved by using the right tool for the job, so the Rosetta Code site is an invaluable resource for thoughtful programmers. There you can compare attempts at solving problems ('tasks') idiomatically in many different languages. I thought some of the gaps in the Go coverage should be addressed… I started off with Smith Numbers - just by rewriting the existing C version in Go. It's interesting to compare the versions side-by-side; it seems clear that the Go version is less cluttered and therefore a little quicker to comprehend. Next Calendar , a classic task I haven't considered since college days. I wrote that from scratch, populating a data structure first, then displaying it. On to Combinations and Permutations where use of the standard math/big library made things very easy. Again, it's interesting to compare the readability of the code with some of the other languages implementing this task. For the A...

Go Can Have Variant Types

…how and why to use them   Here I discuss my use-case for variant types in Go , how to implement them, and the pattern I have established for their safe use. I have included some real-world code rather than the traditional Go “animal farm” examples but for the short version skip to the TD;DR at the end. Background I am working on a large spare-time project which currently consists of a little over 7000 lines of Go, I expect that to double before it is released. I started off inspired by an extant Java project which had related aims, but the Java style was very idiosyncratic and I found the code hard to read. For historical reasons I flirted with PL/1 , and that might have been a goer if only it were a little more mature; unfortunately I hit bugs in the compiler which I didn't want to work around. Then I converted the code to good old plain C which was fine until I wanted platform-neutral threads and filesystem I/O. I know there are libraries to solve these problems, b...