Simple Hello World in GO Lang

Hello all, so you want to become gophers.
In this post we will be create a simple hello world application in GO on windows OS.

I know there are other blog post related to this, but in this blog I will share my experience how to write code in GO
Note : I am from PHP and JavaScript background and have over 5 years of experience but I am relatively new to this language.

  • Install Go on windows
  • Setup Go Path to get most out of it(Optional)
  • Fire up your favorite code editor, for me its VS Code 🙂
  • Begin writing simple golang code

Installing Go in windows

This would we pretty straight forward in windows. Just head to golang official website and download the binary for golang and install it by double clicking and following simple install instruction.

See its not that hard 😉

Setup Go Path to get most out of it(Optional)

You can do this by adding and environment variable GOPATH to your system.

  1. Right Click on This PC and click on properties.
  2. Click on advanced system properties on left hand side.
  3. Click on Environment Variable in System property windows.
  4. Now click on New button under User variables(You can also add it on System path if you prefer, I have setup mine to user level).
  5. Now enter “GOPATH” in variable name and you code’s Source directory in variable value.
  6. Click on OK button to save.

This step may seems unnecessary, but believe me it will save you a lot of time down the drain but start working with other external packages.

Fire up your favorite code editor

Now lets begin writing some code in GO

Now lets create these folder if it does not exists already “src\github.com\username” where username is your github account’s username.

Following this naming conventions helps go to find packages easily.

Note: If you want, you can use your own company’s git domain.

Now create new directory for your project, for this example I will call it a example-hello-world

Now fire up your code editor in this exmaple-hello-world directory

Begin writing simple golang code

This directory will hold all your code for your project. For this simple hello world application, we will create main.go file and write following code.

Now to run this application, open terminal in example-hello-world folder and type go build main.go to see if we have any compilation error.

If you don’t see any error, that means you are good to go. Now to run this application fire the following command go run main.go

Note: You can also see the .exe file generate after you build your application.

Voila! You have just create new application in GO Lang.

In next couple of blogs, we will create some more simple golang programs.
Be sure to check it out.

Leave a Reply