Home > Software Development > Getting Started with Go Programming Language

Getting Started with Go Programming Language

February 2nd, 2015 Leave a comment Go to comments

Go is a relatively new programming language coming out of Google. It’s getting popular with more new projects using it. Two of the container software, Docker and Rocket are both using Go.

At high level, the Go programming language is very much like C but with many improvements. To my curiosity, I started to study Go and get ready for future consulting projects. Here are the note on installation and a few samples along my learning path.

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure



On Ubuntu, you can simply use the apt-get command if you don’t care which version but a stable version of Go to install.

$ sudo apt-get install golang

If you want the latest version of Go as of this writing, you can download and install it as follows:

$ cd ~/Downloads
$ wget https://storage.googleapis.com/golang/go1.4.1.linux-amd64.tar.gz
$ sudo tar -C /usr/local -xzf go1.4.1.linux-amd64.tar.gz
$ export PATH=$PATH:/usr/local/go/bin

For Windows and MacOS, you can find the binaries at the download page: https://golang.org/dl/. There are detailed instruction on installation at this page: http://golang.org/doc/install

Tooling [optional]

Having an editor with syntax highlighting is nice but not required. If you plan to use vim as the editor, you can add syntax highlighting. As it turned out, it’s not easy as it should be. A quick search got me to the vim plugin hosted on GitHub: https://github.com/fatih/vim-go. After playing with it for a few minutes, I could not get it working. It may be because some packages are missing. Anyway, I then tried a simpler solution which worked fine.

In earlier version of Go, there may be go.vim file included at $GOROOT/misc/vim/go.vim. But in the version I had installed as above, there is no such file. But it can be easily downloaded here.

After it’s downloaded, you can copy it to a pre-defined directory:

cp ~/Downloads/go.vim ~/.vim/syntax/

Then in any opened go source code, just type :set syntax=go. The syntax highlighting will work magically. Although it works, it’s not convenient to type it for each file every time. Instead, you want to set it as default for any file with .go extension. Our job is to tell computer what to do, and let the computers do the work. :)

For that, you can add the following line into the ~/.vimrc file. After that, you can get the syntax highlight automatically.

autocmd BufNewFile,BufRead *.go  set syntax=go

Because I use Netbeans these days, I was wonder if I could use the Netbeans for development. With no surprise, there is a plugin already. Just add a new repository as follows and install the plugin just like any other plugin.

Netbeans plugin:
Name: Winsoft
URL: http://www.winsoft.sk/netbeans/updates.xml

I haven’t spent much time on the plugin, but I noticed that it’s not yet as polished as other plugins for languages like Python, Ruby. More specifically, there is no new project type for Go, and you can only add new .go file inside an existing project. Also, you cannot configure Go runtime environment therefore cannot run the .go without leaving the IDE. The systax highlighting for .go source code seems working just fine.

Because the Netbeans plugin is providing pretty much the same as a vim, I ended up using vim editor. For writing a few samples, vim is more then enough.

Hello World

Now we are ready to run the first program which is also the simplest one. It just prints a string.

package main
import "fmt"
func main()
  fmt.Println("Hello Steve\n")

To run the simple program, you don’t need to compile it like C or Java. You can run it like scripts as follows (at least it feels that way :))

$ go run hello.go
# command-line-arguments
./hello.go:6: syntax error: unexpected semicolon or newline before {

Ooops… There is an error. Somehow the Go does not like my opening { starts on its own line, which is my preferred style. I don’t know why the restriction is there. But moving it to the above line worked just fine. Hopefully this restriction will be removed in the future.

$ go run hello.go
Hello Steve

A Few More Samples

After the first one runs, let’s try something meaningful. Most of the samples are adapted from this site.

This sample shows the usage of arrays: how to declare it, how to initialize it, and how to work with two dimention array.

package main
import "fmt"
func main() {
  var ages [10]int
  fmt.Println("ages:", ages)
  ages[0] = 30
  fmt.Println("ages[0]", ages[0])
  matrix := [2][3]int{{1, 2, 3}, {4,5, 6}}
  fmt.Println("matrix: ", matrix)
$ go run array.go 
ages: [0 0 0 0 0 0 0 0 0 0]
ages[0] 30
matrix:  [[1 2 3] [4 5 6]]

This sample has a simple method call that adds two arguments. The Go method also supports returning multiple values like Python.

package main
import "fmt"
func main() {
  sum := add(1, 2)
  fmt.Println("sum: ", sum)
func add(a int, b int) int {
  return a + b
sjin@sjin-dev:~/golang$ go run func.go 
sum:  3

This sample shows how to use struct, which is a programming construct popular in C.

$ cat struct.go 
package main
import "fmt"
type Book struct {
  title string
  author string
  price float64
func main() {
  b1 := Book{title: "VMware VI and vSphere SDK", author: "Steve Jin", price: 59.99}
  fmt.Println("Book title:" + b1.title)
$ go run struct.go 
Book title:VMware VI and vSphere SDK

This sample shows the for loop

package main
import "fmt"
func main() {
  fmt.Println("One way of for loop ...")
  i := 1
  for i<= 3 {
    i ++
  fmt.Println("A different way for same result...")
  for k := 0; k <= 3; k++ {
  fmt.Println("Infinite loop with break ...")
  for {
    fmt.Println("only once and then break")
$ go run forloop.go 
One way of for loop ...
A different way for same result...
Infinite loop with break ...
only once and then break
  1. naresh kumar
    August 18th, 2015 at 06:16 | #1

    Hi Steve Jin,

    Thank you for this beautiful, best tutorial for GoLang. Seriously GoLang’s docs went over head.
    I want to learn more in this GoLang .. Hope to see more tutorial here.

    Thank you,
    Naresh Kumar

  2. August 18th, 2015 at 11:15 | #2

    Glad you like it Naresh, as I exploring it, I will write more.


  1. No trackbacks yet.