Hello World Example With The Go Programming Language

Let’s Make Your First Go Application

“Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.” – Go Team

Getting Our Environment Setup

Choosing the right IDE

I would highly recommend Visual Studio Code (VSCode). This IDE is powerful, fast, offers an infinite sea of plugins and language support, and runs on Mac, Linux, and Windows operating systems. Meaning you can use the same IDE at work, as at home, and for all flavors of projects you might be entangled with. Personally, I have found myself utilizing VSCode more and more for all things.

Here is where to get VSCode.

There are other IDEs to choose from if you would rather not use VSCode. Netbeans is another option, and you could always write your code in an editor like Notepad++ or Sublime.

Installing Go Tools

The next step is to go get the Go tools. You can find the installer by visiting this link. The download should happen automatically. The installer will create a system variable for you but you will need to go set up your GOROOT and GOPATH variables to build and run your program.

These are the variables that instruct Go about where your workspace will reside and where to find Go resources.

In Windows 10, to set up your variables, in the search box type ‘variable’ then select ‘Edit the system environment variables’. This will open a dialog for your variable.

Click ‘New’ for user variables and name a variable ‘GOPATH’ with a value ‘C:\Users\[username]\go’ where [username] is you of course. Create another variable named ‘GOROOT’ with a value ‘C:\Go’. If you already have a command window open you will need to close it for the changes to take effect.

Now you’re done with the setup.

Let’s Make An Application

To make sure we are working where Go thinks we are, make a folder inside C:\[username] and name it ‘Go’. To keep things clean, create a folder inside of ‘Go’ and name it ‘src’, then inside of ‘src’, and name it ‘Hello’. In the end, the directory you will be working out of will be C:\[username]\Go\src\Hello

Next, open up VSCode and select File >> New File. Go ahead and save this file to C:\[username]\Go\src\Hello as ‘HelloWorld’. You will now have one file in this directory called ‘HelloWorld.go’. Perfect.

With the file open inside VSCode, add the following snippet and save your file.

package main

import "fmt"

func main() {
    fmt.Println("Hello World!")

Open up a terminal and go to C:\[username]\Go\src\Hello. Run the command ‘go build’, if you look back in your Go folder now, you will see a Go.exe that the Go compiler has built.

Run the command ‘go.exe’ and you’ll get the output ‘Hello World!’

Congratulations! You’ve just written your first Go application, happy coding!

2 comments Add yours

Leave a Reply

Your email address will not be published. Required fields are marked *