October 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Anatomy of a Google Go Program

  • February 3, 2010
  • By Edmon Begoli
  • Send Email »
  • More Articles »

Array Slices and Automatic For Loops

In the demo program, I process each line of the file as a space-separated record of fields. I first split the contents of the file into lines using the split function available in the strings package:

lines := strings.Split( contents, "\n", 0 );


Next, I split each line of the file into constituting fields:

fields := strings.Split( line, " ", 0 );


The 0 in the invocation of the Split function indicates how many elements should be returned from that function. The 0 indicates that all substrings should be returned.

Slices

I wrote the demo program so that invocation of the Split function returns an array of all seven elements. I did this for demonstrational purposes, so I can showcase one of the more interesting features of Go's array processing: slices.

Slices allow for arbitrary slicing of arrays. In this case, to access only the first five of the loadedField array, I cut out the desired parts of the array using the following slice syntax:

fields = loadedFields[0:4];


Note that the remaining elements outside of the slice still remain allocated until the entire array is not referenced and available for garbage collection.

Concurrency, Channels and Goroutines

Concurrent programming in Go is very straightforward. The simplest form (and the one demonstrated here) is to execute a regular function as a concurrent routine using the keyword go. In Go, such routines are called "goroutines."

In the demo program, I process each file line concurrently. So, to "fork" off a new goroutine, I call the function acct.CalculateBonus prefixed with the keyword go:

go acct.CalculateBonus( line, sum );


Note the second parameter to the function, sum. This is a channel variable of type float. Channels are pipe-like constructs used to pass values in a thread-safe fashion between two or more concurrent routines.

Recall this code I mentioned previously:

sum = make( channel float, 50 )


Here, sum is declared as a channel of float types that can hold 50 of the variables at the same time in the channel. In other terms, sum is a synchronized buffer of capacity 50.

The invoked acct.CalculateBonus routine places the results of the calculation into a sum channel, like this:

Acct.CalculateBonuses( line, sum ){
 
bonus = line[ 4 ] * 0.25 * 4000;
sum <- bonus;


To retrieve the value from the invoking routine, I pull the value from the sum channel with this command:

totalPaid += <-sum;


Where Will "Go" Go from Here?

I have demonstrated here a very brief survey of some common but very useful features of the Go programming language. These few demonstrated features are by no means all that Go has to offer, but I hope they effectively illustrate the nature of the language as well as its philosophy of implementing features from both well established as well as the most recent existing languages.

Go is a modern, fast and concise language that will appeal to most developers. The question with Go is not whether its syntax is elegant or its features rich but rather whether it has a future. No one can tell yet if Go will enjoy broad enough adoption to become a mainstream language. Given that it is very nicely designed and sponsored by Google, it certainly will enjoy adoption at least at Google. However, a fledgling programming language truly comes into its own when the broader programming community embraces it. Therefore, it is up to us developers to explore this language and its features and adopt it if it so fits.

Code Download

  • GoProgram_src.zip

    For Further Reading

  • The Go Programming Language homepage

    About the Author

    Edmon Begoli is a software architect with 14 years of professional experience on large commercial and public software projects. He is a member of the R&D staff at Oak Ridge National Laboratory.

  • Tags: Concurrency, Google, Compiled languages, Go programming language



    Page 2 of 2



    Comment and Contribute

     


    (Maximum characters: 1200). You have characters left.

     

     


    Sitemap | Contact Us

    Rocket Fuel