Writing your First Program (using Golang)

Posted on May 30, 2020

This article is very beginner-friendly.

Not to worry if you have never written a line of code. It is targeted at people who have zero experience in Software Development. I try to be as technical-jargon-free as possible.

Go or as it is very famously called; “Golang”, is a relatively new programming language developed by Google as an infrastructure for building modern software. Go was designed to handle stuff very efficiently and relatively blazing fast. Some of the key selling points of Go include:

  • An Extensive standard Library
  • Concurrency Built-in
  • Fast Compile times
  • Strongly typed and Garbage collected

To follow along with this tutorial, you’ll need the following:

  • Go installed on your computer(click Go if you do not already have it installed on your computer)
  • An IDE(Integrated Development Environment), I recommend VS code.
  • The Golang plug-in for VS code.

The Go extension

Now that we are set, we would create a go file in VS code and name it main.go, then open the command line interface pressing the ctrl +` for PC and command + ` on Mac OS on the keyboard.

creating a go file in vs code

creating a go file in vs code

“Code is like humor. when you have to explain it, it’s bad” -Cory House

We will be building a Guessing Numbers Game by telling the compiler to generate a random number, say between 0 and 10, then have us guess the number. We will have a limited number of trials which we would also specify in our code.

Now that we have our set up out of the way, Let’s write some code.

First, we declare our package name.

In Go, source files are organized into system directories called packages, which enable code reusability across the Go applications. The naming convention for Go package is to use the name of the system directory where we are putting our Go source files.

In this case, Package main since we will be writing our whole code body in just a single file. we declare it as the main package. Pretty explanatory right?

main package declaration

Then we go-ahead to import packages from the Go standard library that are necessary for building our game, The “fmt” package for formatting text on our screen, The “math/rand” package for generating a random number and The “time” package for telling instantaneous time when we generate a random Integer, this helps us generate and hold one random number at a time.


We declare two variables at first, guess of type int(Integer) stores whatever guess we input when we’re prompted to guess a number and trialcount, which stores the number of times our compiler has received a guess, we initialize this variable as 1 and it is incremented by one after every guess input.

A variable is a storage unit of a particular data type. You can give assign a name (label) to this storage unit. A variable must be defined with the type of data it is holding. For example, string is a data type provided by Go to store character or text data.

Declared variables

Now we declare our main function, “func main” which takes in no arguments ().

A function, in general, is a small piece of code that is dedicated to perform a particular task based on some input values. … In Go, a function is defined using func keyword. func dosomething()

Then we declare some variable of type *rand (from the math/rand package we imported earlier), source- which helps us hold an instantaneous time, randomizer- which tells our compiler to pick a random number at the instantaneous time earlier stated in the source variable, and finally, secretNumber of type int(Integer) which tells our randomizer variable that our random number should be between 0 and 10 and also stores this random number.

main function

Now we create a “for loop” which as the name implies loops through a set of instructions.

A ‘for loop’ is a golang statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a go program.

It tells our compiler to request an integer from us by printing “Guess the number”, scans our input and saves it as an instance of our previously declared “guess” variable. It then checks to see if our trialcount variable has an integer up to 5 stored in it at which point, it prints “You lose” in the console, and then the “break” keyword tells the compiler to stop and end the loop.

If not, it checks the input to see if it’s more than our randomly generated integer. If our guess is greater than our random number, it prints “Too Big” to the console, then adds 1 to our trialcount variable, if it’s less than our Random number, it prints “Too Small” this also adds 1 to our trialcount variable, if neither is the case, then we have succeeded in Guessing the Number :D !!!!!!!!!

We can now go to our terminal to test our newly written code. We run the code by entering the “go run main.go” command in the command line interface. Here’s an example with me trying the game, I won! ;-)

Terminal example, I Wonn!!!

You can check out the Github repo for the working source code we just wrote.

Please clap and share, someone might just need this push to start their software developer journey. Also, follow me on twitter:), you can dm me if you feel there’s something I can improve on in this article, I also intend to start ranting about consumer tech, that would be an avenue for me to manifest my Apple fanboy-ism, hopefully by the time you’re reading this, I’ve stopped procrastinating.


There exists limitless opportunities in every industry. Where there is an open mind, there will always be a frontier.

I would recommend these tutorial videos for further learning if you will, which I recommend you do because as I’ve come to learn in my very short journey learning how to code, it opens your eyes to endless problem-solving possibilities.