Software Development

Go for Java Programmers: Introduction

Background

Go (often referred to as “Golang”) is a fairly new programming language, first conceived in 2007, with version 1.0 launched in 2012.  Its three inventors are currently Google employees, with impressive credentials.  Ken Thompson is the legendary father of UNIX.  Rob Pike created the influential Plan 9 operating system alongside Thompson, and Robert Griesemer worked on the Java HotSpot virtual machine and Google’s V8 JavaScript engine.

The origins of Go stem from a belief that C++ has grown too complex. The three inventors wanted a new systems-level language… likewise building upon a C foundation, but adding contemporary concepts and eliminating cruft. Like C, Go has a fairly compact language specification, and a small standard library compared to other modern languages. gopher-colorHowever, Go introduces features such as:

  • Static typing and type safety
  • Automatic garbage collection
  • Simplified use of pointers
  • Built-in support for concurrency
  • A dependency management system
  • … and much more

It was originally assumed that Go’s target audience would be C and C++ developers.  However, so far the most enthusiasm has come from Python and other dynamic language communities.  Programming in Go “feels” somewhat similar to Python in many ways… adding support for static types, easy concurrency, and ridiculously faster execution speed.  It can be described as a “best of both worlds” model, combining the efficiency of a low-level systems language with the ease and expressiveness of much higher-level languages.

Go for Java Programmers?

In a sense, the motives behind Go are similar to those behind Java’s origins.  Twenty years ago, Sun Microsystems wanted a programming language that was easier and more forgiving than C/C++ (e.g. garbage collection, no pointer arithmetic!).  dukeYet it had to be powerful, with a rich standard library sufficient for most tasks.  The industry was just entering the commercial Internet age, and needed language with strong networking support build into its foundation.

I have been working with Java professionally for over 15 years, and I don’t see its status as my primary language changing anytime soon.  However, I have always had need for a secondary language to serve where Java is a poor fit.  Sometimes I need a quick script, to parse through several gigabytes of dirty log files.  I need a one-off command line utility, for feeding information into a database.  I need to easily deploy some batch processing logic, to be invoked from a cron job.  I might need a basic CRUD web app, simply wrapping a few database tables for easy data entry.  Etc.

Of course, I can do all of these things in Java.  It’s just that using Java for many of these tasks is like using a chainsaw when you need a pair of scissors.  Compared to other languages, it takes a good amount of work to bootstrap a Java program.  You have to either set up a build system such as Maven or Gradle, or else manage your JAR file dependencies by hand.  Deployment for a small special-purpose utility feels like overkill.  Deploying JAR’s separately means having to manage your classpath when invoking the JVM, whereas bundling them into a monolithic executable JAR can be problematic.

More importantly, Java is a very verbose language by today’s standards.  I don’t consider this a drawback for large team-oriented projects, or for complex logic meant to be maintained for a long of time.  I find that criticism comes from newbies trying to use Java for simple, short lived, one-off tasks… where the verbosity is unnecessary overhead.  However, opening and parsing a bunch of files does require a frustrating amount of boilerplate code, if that’s all you’re needing a small utility to do.

Therefore, like many Java programmers, I’ve always had a secondary language or two in my back pocket for such side tasks.  Back in the late-90′s, it was Perl.  That eventually shifted to Python, although I’ve also spent significant amounts of time with Groovy, Scala, and others.  These extra languages have not only filled Java’s gaps for special side tasks, they have also served to improve my Java development skills.  When I started working with asynchronous Java code, it was easier due to having previously worked with JavaScript (where asynchronous is the norm).  If I had not already been working with Scala for several years, then learning the new functional constructs in Java 8 would be a far more daunting task.

Go has become my primary “other” language over the past year.  It offers several great advantages for a Java programmer:

  • A light and clean programming style, that feels like working with a dynamic language, yet provides the compile-time type checking that Java developers prefer.
  • A standard library that is small enough to quickly learn, yet rich enough for all the things you might commonly need.  There are built-in parsers for the most common data types (e.g. XML, JSON, CSV, etc).  A database library that resembles JDBC.  A built-in HTTP server with excellent performance, for writing quick RESTful services or small web apps.  Etc, etc.
  • Easy dependency management and deployment.  Go programs compile directly to native executables, with all dependencies statically linked.  There’s no virtual machine!  You don’t have to worry about having the correct version of Python installed on the target box, or wrestle with multiple environments on your own box using tools like virtualenv.  Just build an exe, and run it.  Although executables are platform-specific, Go makes it easy to cross-compile… building a Linux executable on a Windows workstation, for example.
  • Not only are Go executables fast, but so is the compiler.  The compiler is so fast, that you can run Go programs as “scripts”, with the compilation occurring silently under the covers.
  • One of the easiest models out there for writing highly concurrent code in a safe manner.

Learning Go as a Java Programmer

Although Go is a fairly light and easy language to learn, there are some cultural challenges in approaching it as a Java developers.  There are several excellent books on Go (see the Resources section at the bottom), but they are mostly written from a C/C++ perspective.  Many of the things they emphasize are alien and distracting to Java programmers.

Meanwhile, there is IRC and StackOverflow and many other online Go communities, but these seem largely populated by Python developers.  They can contrast Go’s environment and build tools to “pip” and “virtualenv”, but are not as able to explain them relative to Maven and other Java concepts.

So in the weeks and months ahead, I plan to write a series of blog posts about Go, featuring different aspects from a perspective helpful to Java programmers.  I will compare Go concepts to their Java counterparts.  Hopefully this will be more helpful to Java programmers than reading explanations meant for a C/C++ or Python audience, and having to mentally translate that into Java terms.

Links to all of the post in this series, as well as topics planned for the future, are listed on the main Go for Java Programmers series page.  If there are any addition subjects that you would like to see covered at some point, please don’t hesitate to contact me with your suggestions.  Thanks for reading, and enjoy your experience with Go!

Additional Resources

  • A Tour of Go – An interactive browser-based tutorial, for learning the basics of Go through live code examples.  Showcases the Go Playground, a means for testing out Go code in a web browser, hosted on Google App Engine.
  • Effective Go – A 50-page survey of language features, ideal for reading after you complete the Tour of Go tutorial.
  • How to Write Go Code – A brief overview of environmental topics, such as library management, and the proper directory structure for a full Go application.
  • An Introduction to Programming in Go – A 165-page book by Caleb Doxsey, available for free on his website, with an Amazon Kindle version available for nominal charge.  I have previously written a detailed review of the book on this site.
  • The Go Programming Language Phrasebook – This 288-page book by David Chisnall has an odd title, since it certainly isn’t a mere “phrase book”.  Instead, it’s a well-written mid level overview of the language.  It’s scope and density falls somewhere in between Doxsey’s book above, and Summerfield’s book below.
  • Programming in Go: Creating Applications for the 21st Century – At 496-pages, Mark Summerfield’s book is the most thorough deep-dive of the language currently on the market that I would recommend.  If you ever learned Perl back in the day, then you can think of Chisnall’s book as analogous to the “llama book“, while Summerfield’s is analogous to the “camel book“.

In the next article of the Go for Java Programmers series, we’ll compare the differences in basic syntax between Java and Go… starting with packages, functions, and variables.
 

Reference: Go for Java Programmers: Introduction from our JCG partner Steve Perkins at the steveperkins.net blog.

Steve Perkins

Steve Perkins is the author of “Hibernate Search by Example”, and has over fifteen years of experience working with enterprise Java. Steve lives in Atlanta, GA, USA and currently works as a software architect at BetterCloud, where he writes software for the Google cloud ecosystem.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Tachyon
Tachyon
4 years ago

System language with a gc ? Oh, please.

Back to top button