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.
Run the program as:
chetan:go-work chetan$ go run hello.go hello, world
And done, we have a simple working Go program! 🙂
Cool, now let’s talk about the editors. There are multiple editors that support Go, here are a few of them:
- vim-go – plugin provides Go programming language support
- Atom – Go-Plus is an Atom package that provides enhanced Go support
- Visual Studio Code – Go extension provides support for the Go programming language
In this blog, we will setup Go on Visual Studio Code:
- Download and install Visual Studio Code from https://code.visualstudio.com/
- Click on the ‘Extensions’ icon and search Go for the extension
- Use the extension by Microsoft and install it (I think this will also install delve to debug the Go apps)
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.