Herman Schaaf

Programmer and traveler

Read this first

Diary of Building an iOS App with React Native

When news broke that React Native was open sourced on Friday, I felt elated. This was the moment I had been waiting for. Ever since the React.js conference videos introduced React Native in late January, I couldn’t wait to get my hands on it.

If you don’t know React Native, it’s a new open source framework by Facebook that allows you to write iOS (and, eventually, Android) apps using HTML-like code (called JSX) and JavaScript. It’s based on the popular React.js JavaScript framework, but you can’t run normal React.js code directly as an iOS app. Instead, once you are familiar with the basics of React.js, you can use the same knowledge, but slightly different components, to assemble iOS and Android apps. They refer to it as “Learn once, write anywhere”, as opposed to the “Write once, run anywhere” approach advocated by other frameworks, like PhoneGap. The paradigm employed by React

Continue reading →


Excellent Open Source Go Projects

Something I often hear new Go developers say is that they are looking for some good projects to study, learn from and contribute to. Normally I suggest reading the Go source: it’s easy to read, you can pick a part you are interested in, and is probably bound to be the best example of idiomatic Go. But this past weekend presented itself with an opportunity to find more open source Go projects that are of the highest quality.

I participated in Gopher Gala, a 48 hour-long worldwide hackathon for Go. Our team built a project called Go Report Card, and it aims to rate the quality of any open source Go project on Github. The tool uses various other Go tools: gofmt -s to judge basic formatting, go vet to check for suspicious constructs, gocyclo to measure the cyclomatic complexity of the code, and so on. It then averages these results to get the final grade.

My teammate Shawn Smith and I have

Continue reading →


Solving Regex Crosswords using Go

ezgif.com-optimize.gif

I first discovered regular expression crosswords two weeks ago, when RegexCrossword.com appeared on the Hacker News front page. I thoroughly enjoyed the nerdy 30 minutes I spent doing the puzzles, but soon enough my natural inclination towards making computers do my hard work got the better of me: I wanted to solve this with code. This morning I had some free time and decided to give it a shot. It was perhaps even more fun, and definitely more exciting, than solving the puzzles by hand!

The end result is about 150 lines of Go code that solves most puzzles in only a few milliseconds. By showing you how I solved it, I hope you’ll learn some interesting things about solving puzzles programatically, how regular expressions work and specifically, how they work in Go. Let’s dive in.

 The algorithm

My first instinct was to solve this with brute force, by iterating over every possible

Continue reading →


Running the Go Race Detector with -cover

There are two test options that make testing in Go especially powerful: one is -cover, which generates test coverage reports, and the other is -race, which warns of possible race conditions in your code. If you haven’t used them yet, you should. They are very useful tools, and both are covered well (so to speak) by official blog posts: The cover story and Introducing the Race Detector.

A subtle problem arises, however, when you use these two together. Suppose we have this simple function that adds one a hundred times inside separate goroutines and returns the result of this summation:

package coverrace

func add100() int {
    total := 0
    c := make(chan int, 1)
    for i := 0; i < 100; i++ {
        go func(chan int) {
            c <- 1
        }(c)
    }
    for u := 0; u < 100; u++ {
        total += <-c
    }
    return total
}

We can place the function in a file called

Continue reading →


Efficient String Concatenation in Go

In this article I investigate the computational performance of various string concatenation methods in the Go programming language.

To evaluate the options, I prepared some typical Go benchmarks using the Go testing package. A benchmark looks something like this:

func BenchmarkBufferString(b *testing.B, numConcat int) {
    var ns string
    for i := 0; i < b.N; i++ {
        next := nextString()
        buffer := bytes.NewBufferString("")
        for u := 0; u < numConcat; u++ {
            buffer.WriteString(next())
        }
        ns = buffer.String()
    }
    global = ns
}

For the purposes of these benchmarks, I imagined having a process that returns string segments one by one, and these segments need to be concatenated to form one string. To represent such a process, I created a simple iterator that returns the string version of its current index on each call:

// nextString

Continue reading →


Why is Golang popular in China?

Search for "golang" on Google Trends

Earlier this week I wrote a post titled The Popularity of Go. The post itself turned out to be quite popular, and I received a lot of great feedback from the Go community (thanks!). One thing I brought up in the post, and that a lot of folks commented on, was the apparent popularity of Go in China according to Google Trends. The graph above shows the searches for “golang” by country on Google Trends. Impressive, right? So many more searches for “golang” in China than anywhere else in the world. Like many others, I too wanted to know why. I’m not much of an investigative journalist, but I decided to do some of my own digging anyway. Is Go really that popular in China, or are we seeing skewed data? And if the data is skewed, why, and how much?

One redditer suggested that, perhaps, “golang” is in fact a word in a dialect of Chinese. Being able to speak and read Mandarin Chinese myself, I

Continue reading →


The Popularity of Go

go_trend1.png

When you look at the Google Trends graph above, it would seem like the Go programming language, also known as Golang, is on its way to big things. But let’s not get ahead of ourselves, Go fans. Let’s first have a cup of java to put things into perspective:

golang_trend2.png

Golang’s popularity as a search term might be growing faster than bamboo in springtime, but it’s still dwarfed by the massive overgrowth that is the Java programming language. But interestingly, we can see in the graph how dramatically Java’s popularity has fallen in recent years. Maybe it has something to do with the worrying decline in programming interest?

go_trends3.png

Hmm. Not sure what to make of that, considering my career choices. Anyway, while Go’s popularity is on the rise, it’s worth pondering whether it will ever grow to the heights that Java once enjoyed. It doesn’t look likely to happen any time soon.

Another interesting

Continue reading →