I’ve decided to experiment with 12 new programming languages or frameworks over the next year. Just like spoken languages, mastery of a coding language can take years even with daily use. With just one month per language I hope to develop a basic understanding of the languages strengths and weaknesses. Languages are tools that help us solve problems and having a broad perspective of what’s in the toolbox can simplify our solutions. Lots of problems can be solved with C, but why use it for the subset that Python can solve faster and more reliably?
All programming languages have a unique feel. Python fells concise and lucid whereas C++ feels bulky and ridged. For August’s language I picked Google’s Go. I selected Go because it supposedly has the feel of a dynamic interpreted language with the speed and type safety of a compiled language.
To get a feel for the syntax I wrote a simple closure that returns two functions, one for adding a list of integers to the sum and one for calculating the average. As you can see, Go certainly has the feel of a scripting language but requires variable type declaration. Like Python it also allows for returning multiple results from a method.
// Note: WordPress doesn't have a 'Go' syntax highlight yet so I'm using javascript package main import ( "fmt" ) //Closure that returns a sum and average method func stats() (func(a []int) int, func() float64){ sum := 0 entries := 0 log := func(){ fmt.Printf("sum: %d, entries: %d\n",sum,entries) } return func(l []int) int { defer log() for i:=0;i<len(l);i++ { sum += l[i] entries += 1 } return sum }, func() float64 { return float64(sum)/float64(entries) } } func main() { sum,avg := stats() s := sum([]int{1,2,3}) s = sum([]int{1,1,1,4,1}) fmt.Printf("sum: %d avg: %f\n", s, avg()) }
As a long time C programmer I initially found the reversed type and declaration syntax to be awkward, but after writing a few hundred lines it actually flows nicely. Rob Pike put together a great three day tutorial that you can access online or in the /go/doc directory. For more information on the syntax check out the day one slides. By far the most compelling feature of Go is the built in support for concurrency. As someone who has done lots of multi-threaded and multi-process development I believe the Go development team did a fantastic job at making it a fundamentally ingrained principle. To illustrate this feature I wrote a concurrent Pell Number generator below.
package pell // Pell Numbers // 0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378 // P0 = 0, P1 = 1, PN = 2PN-1 + PN-2 // Day 3: Do not communicate by sharing memory. // Instead, share memory by communicating. func Pell(ch chan<- uint64) { var fn_1, fn_2 uint64 = 1, 0 ch <- 0 ch <- 1 for { fn_2, fn_1 = fn_1, ((2*fn_1) + fn_2) ch <- fn_1 } }
There is nothing special about the above Pell routine. Its parameter is a communication pipe (called a channel in Go) that guarantees synchronization because it is a blocking write. To start the thread you use the “go” keyword and read data from the same channel passed to the routine. Upon a read it unblocks the thread which then calculates the next number in the sequence.
package main import ( "fmt" "flag" "pell" ) func main() { c := make(chan uint64) var iterations *int = flag.Int("i", 10, "number of iterations to run") flag.Parse() go pell.Pell(c) for i:=0;i < *iterations; i++ { var num uint64 = <-c fmt.Println(num) } }
From my perspective the one major red flag is the lack of support for object oriented design. Although the ability to add a method to a struct or any data type is very cool I don’t yet see how it can fully replace the concept of a class. I added a small miscellaneous Go repository on gitHub that anyone interested in some social coding can fork and modify at will.