Collaborator8.2-Blog-CTA-Demo

An Introduction to the Go Language: Boldly Going Where No Man Has Ever Gone Before

frontpage

The Go Programming Language (Go) is an open-source programming language sponsored by Google and created by Robert Griesemer, Rob Pike, and Ken Thompson that aims to simplify systems programming and to make programming fun again. This article aims to introduce developers to Go and give them enough information to try it out for themselves.

Go is a relatively new language that is in constant development but it already has a considerable amount of industry support and is used in real systems at Google.

The notion of yet another programming language may seem weird to some people. But Go does bring something new to the table and its fundamental design principles make it different enough (and arguably, better enough) to justify its existence.

Hello World!

No introduction to any language is complete without the canonical Hello World program:

package main

import "fmt"

func main() {

    fmt.Println("Hello world!")

}

The program defines a new package (main is always the package that contains the main function) then imports the input/output formatting package (fmt), defines its main function (which is the program’s entry point), and uses the Println function from the fmt package to print the string Hello world!.

Why Go?

Go has several features that make it unique and attractive. Here is a non-exhaustive list:

  • Although it is compiled, Go is also a garbage-collected language. This puts less pressure on the developer to do memory management, as the language itself takes care of most of the grunt work needed. In this respect, it is similar to Java, as opposed to C++.
  • Go has built-in concurrency, which allows parallelism in an easier way than is possible in other languages. Go has the concept of goroutines to start concurrent work and the concept of channels to permit both communication and synchronization.
  • Go is a statically-typed language (which makes code more robust as several possible bugs can be detected during compilation) that supports duck-typing (types can satisfy interfaces and be passed in to any functions/methods that accept those interfaces). Go also has type inference, which means you do not need to explicitly mention a variable type whenever it can be inferred by the compiler.

Installing Go

Go can be downloaded from the project page and there are pre-packaged versions for several platforms (Linux, FreeBSD, Windows, MacOS) and architectures (ARM, x86, x64). Full instructions can be found here.

As it is open-source, you can also download its source code and compile it yourself.

Go by Example

From all of the things mentioned above, it should be evident to would-be Go programmers that concurrency support is deeply integrated in the language. This helps making concurrent programming a lot easier. Consider this example from a recent Go talk:

package main

type Ball struct{ hits int }

func main() {

   table := make(chan *Ball)

   go player("ping", table)

   go player("pong", table)

   table <- new(Ball) // game on; toss the ball

   time.Sleep(1 * time.Second)

   <-table // game over; grab the ball

}

func player(name string, table chan *Ball) {

   for {

     ball := <-table

     ball.hits++

     fmt.Println(name, ball.hits)

     time.Sleep(100 * time.Millisecond)

     table <- ball

   }

}

You can run it by yourself and play with it here.

This programs defines a channel (table), starts two players concurrently (“ping” and “pong”), and then starts the game by sending a ball to the table.

Each player tries to grab the ball from the table; only one succeeds. The player that grabbed the ball increments the hit count and sends it back to the table. The other player then grabs it and does the same. This repeats for 100 milliseconds when the main routine grabs the ball again and the program terminates.

Go’s Future

Recently Go 1.1 was released. This version brought significant performance improvements.

Although Go is in constant development, the existing API is stable and guaranteed not to break in any meaningful way throughout all Go 1.x releases. This gives developers a stable platform to work on, which is specially important for large scale projects.

Go is a great language that is improving fast and that showed it can be used for all project sizes. There is already a lot of open-source third-party code that can be used to complete the most varied tasks and a sizeable community to help with all kinds of questions.

Obviously, I’m a fan. Together with everything else mentioned here, Go is definitely an option that you should consider.

See also:

Comments

  1. Jessica Darko says:

    Go does not have “concurrency” or “parallelism”. It is a terrible attempt at solving these issues, especially considering that erlang solved them correctly over 20 years ago.

    It’s sad that so many so-called “engineers” arent’ willing to put in a little effort to learn erlang and thus produce correctly engineered results, but would rather take something that seems like no-effort and use Go and get a program that doesn’t actually work.

    You cannot do concurrency or parallelism correctly in go. It simply isn’t possible with a shared memory architecture (and lacking the other fundamentals of erlang)

    Anyone who thinks otherwise is incompetent.

    Go didn’t have to be this way– building a new language they could have done it right, but the people at Google are incompetent as well.

    And a sure sign that we have people choosing languages based on fashion rather than competency is that the first response to this is going to be a sneering one claiming that it’s asinine to say google is incompetent (Because incompetent programmers worship google and don’t know the difference between enginering and a stupid company worshiping ideology.)

    Anyway, shame on you.

    • Jesus Christ lady, it’s just a language.

    • Seems like you’ll make a really good dictator, have you considered a career change?

    • This post is just you shouting, “My old thing is better than your new thing!” over and over. How about you come back and and some technical details and examples of Go programs that don’t “actually work”.

    • I am as much a fan of Erlang as the
      next person and have written a couple non-trivial applications on top
      of it that work and behave as I intended. Like you, I am inclined to
      believe that it solves the issues of concurrency in a well balanced
      way, providing the right abstractions and metaphors to reason about
      dataflow in a complex systems.

      That said, writing off an entire chunk
      of the developer population as “incompetent” does little to drive
      the conversation forward in a meaningful way. The majority of your
      post is composed of ad hominem attacks and fallacious appeals to
      authority, giving little evidence of any truth in your backing.
      Aside from that, you do little to actually support your assertion of
      Erlang’s capabilities, outside of a hand wavy mentioning of shared
      state without validation.

      Is it too much to ask that we leave
      rhetoric at home and engage in a well reasoned debate on the pros and
      cons of a given language? I personally find Go to be less
      interesting than other languages and, while it doesn’t particularly
      interest me, it’s sloppy to dismiss it out of hand without weighing
      it on its own merits.

    • Bruno Albuquerque says:

      Hi Jessica.

      it seems you have pretty string opinions about every single language in the world versus Erlang. I am particularly of the opinion that Go is a pretty good language but the best programing language will always be the one you are more confortable with. A programming language is a simply a tool and is not an end on itself.

      That being said, your assertion that Go does not have concurrency or parallelism is simply not true. Based on this assertion alone I wonder if you actually tried to use the language or is just refusing to use enything that is not Erlang.

      One of the tenets of Go programming is “Do not communicate by sharing memory; instead, share memory by communicating” which at least partially addresses your concern about “shared memory architecture”.

      An interesting read: http://blog.golang.org/share-memory-by-communicating

      But what do I know? I am incopetent, it seems (at least based on your definition of incompetence).

    • Yngvar Kristiansen says:

      Nobody can take what you say to be valid, as you don’t have any sources nor proove anything.

  2. Bruno Albuquerque says:

    While we are at it, I just discovered Python also has a web server (I never used it, so I do not know how complete it is) on its standard library. Living and learning. :)

  3. Sorry, fail to see any compelling reasons to invest in Go, unless I decide to become a Google imp in the future.

    Why Go:

    1- it is fast: So what? any compiling lang is. about the build time: faster than D? even in c++ you can use precompiled headers, making casual rebuild in 1sec or parallel build for a huge proj.

    2- garbage collection is not a plus! it is a disadvantage that a language is not allowing for direct memory management.

    3- as far one model fits all :( I doubt it will be ‘optimal’ for many situations, where indeed it is critical to have concurrency. Erlang is also 1 model fit all, take the message passing or leave it :) thats not a ‘correct’ solution, it is simply one solution!

    4 – nothing unique about string typing and having ducktyping. take any dynamic language with strong-type support!

    5 – I prefer less comments/doc more clear code!

    6 – built-in web server :) on this one I will leave it with a smile :) Indeed it is nice, but very questionable why not simply use lib/framework for something that has nothing to do with language design to start with :)

    7 – CMAKE & Jenkins is good enough.

    • Bruno Albuquerque says:

      Let me try to address your comments:

      1 – Did you actually try Go? Compilation times are orders of magnitude faster than a comparable C++ project. With or without pre-compiled headers. Let me show you a concrete example:

      bga@librarian:~/go-dev/src/github.com/stretchr/goweb/example_webapp$ time go build main.go

      real 0m1.193s
      user 0m1.160s
      sys 0m0.200s

      goweb is a web framework for Go. It has lots of dependencies and all of them got built when I ran the command line above. That’s roughly 15000 lines of code. Even on my very underpowered ARM NAS, it gets build in 8 seconds.

      2 – I guess that is a matter of preference. There are advantages and disadvantages of using a garbage collector. For most programmers, having it is actually a plus.

      3 – Where did I say that one model fits all?

      4 – Each feature taken isolated will obviously end up being matched in some other language. The fact that they are all in the same lamnguage is what matters.

      5 – Having an easy way to document programs does not make the code less clear, so this is a moot point.

      6 – That’s not part of the language, it is part of its standard library. Having a rich standard library helps programmers get the job done without having to hunt for specific things.

      7 – I believe that it is true in your opinion. But I really think you should try Go first before reaching to conclusions like that.

      Other than that, programming languages are simply tools.They are a means to an end and not an end in itself. If is fine to prefer one language over the other but I do not think it is fine to rule out any language without trying it first..

      • 1- not interested

        2- I’m not against GC. I’m against GC only! Go,C#,Java etc.Hint, D language is way more interesting than Go in that respect giving you the best of both world.

        3- it was both comment for Erlang&Go where concurrency is “build in” but usually it is one realization fit all ! e.g. message passing for comm.

        4- true! but again is it unique to Go?

        5- NO! but it makes it bloated! e.g. #region in c#

        6- Again! how different is then from I must say almost all the rest?

        7- true! but which one first..? D, Go, Ada, LuaJIT … damn they are all good :) yet none will replace C/C++ ,cause it is not merit only. if popularity of lang. was merit only, JavaScript should had never met a daylight ;)

        Agree! You can learn something from any language! without exceptions, even JS :P
        but one can not spend 100% of its time on 100% of the languages. one needs to learn to discriminate on the fly and prioritize accordingly. I’am into compiled lang, but unfortunately Go is just after D and Ada on the list. Mainly because I fail to see a killer feature, in contrast to D,Ada. Considering the fact I can hardly focus on more than 1 , Go is left in my periphery for now.

        after all, it is MHO
        Cheers,
        p.s. no offense

        • Bruno Albuquerque says:

          Well, you can obviously decide what language to use or not, but judging a language and giving an opinion about it whithout really trying to use it does not make any sense. So i guess this discussion won’t really go anywhere and we will have to agree to disagree.

    • > So what? any compiling lang is.
      >….
      > take any dynamic language with strong-type support

      And just how many dynamically, strongly typed compiled languages are you familiar with? You’re missing the point that Go combines good features from many different types of languages.

      >garbage collection is not a plus! it is a disadvantage that a language is
      >not allowing for direct memory management.
      >…
      >I prefer less comments/doc

      Now just tell me that type inference is either impossible or a fad and you could be a Delphi programmer….

      Memory management is deterministic… delete something that’s no longer being referenced, otherwise don’t. The only thing a programmer can do manually is screw it up.

  4. onjoFilms says:

    Looks like ‘C’ to me.

  5. My brother suggested I might like this website.
    He was once entirely right. This post actually made my day.
    You cann’t believe simply how a lot time I had spent for this information! Thank you!

Trackbacks

  1. [...] в язык Go: сильные стороны и первые [...]

  2. […] в язык Go: сильные стороны и первые […]

Speak Your Mind

*