Golang Hello World!

Now that we installed Go on our Mac and Ubuntu systems in the last blog, it’s time to run our first script – program to display hello, world and learn about editors that support Golang. We will download and setup the Visual Studio Code editor.

Go Hello World

Running a Go script is very similar to executing any program for an interpreted language. Like in Python, we execute a script as python app.py, in Golang we can run a script with the command go run hello.go.

Let’s write a simple Go program to display hello, world on the terminal. Open an file hello.go and type in below source code.

At a high level – package keyword indicates this file can be used as a package for building command binaries and main() method gets called when the program is executed. fmt is a library for formatting output data on command line.

Source code:

Screen Shot 2018-12-04 at 11.51.33 AM

Run the program as:

chetan:go-work chetan$ go run hello.go
hello, world

And done, we have a simple working Go program! 🙂

Go Editors

Cool, now let’s talk about the editors. There are multiple editors that support Go, here are a few of them:

  1. vim-go – plugin provides Go programming language support
  2. Atom – Go-Plus is an Atom package that provides enhanced Go support
  3. Visual Studio Code – Go extension provides support for the Go programming language

In this blog, we will setup Go on Visual Studio Code:

  1. Download and install Visual Studio Code from https://code.visualstudio.com/
  2. Click on the ‘Extensions’ icon and search Go for the extension
  3. Use the extension by Microsoft and install it (I think this will also install delve to debug the Go apps)

Screen Shot 2018-12-04 at 12.14.56 PM

That’s it! So now we have an editor setup and we can also run a simple Go program from the terminal.

In the next blog, we will look at the Go project structure, understand packages and commands. We will also develop our own package and learn how to work with third party packages.

Leave a Reply

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