Learn Golang by Solving Real-World Problems
Tags: go, beginners, programming, vscode
Published: 2020-11-21 09:55:26

####Roadmap To Mastering the Language


---


Since you are already sold on learning "Go" after reading countless articles on the internet, we will cut it down directly to how you will learn it. **In this series of blogs, we will learn anything new by solving real-world problems via programming and down the line, will focus on its applications.**

We will start our journey by learning the keywords and syntax in Go. Later, we will move on to conditionals and statements, functions, methods, structs, maps, and many more features of the Go programming language. We will also cover how to read the go standard library because believe me it is confusing and time-consuming to just get the gist of what it wants to convey when you are a beginner.

**Following is the roadmap of what we will be doing under various categories.**

![Note: This roadmap may change further down the line but everything which is shown in the photo will be covered. The only thing that may change will be the sequence or new things which I think should be added.](https://cdn-images-1.medium.com/max/4240/1*s7NLuA0xy4G5vrv261ExzQ.png)*Note: This roadmap may change further down the line but everything which is shown in the photo will be covered. The only thing that may change will be the sequence or new things which I think should be added.*

### Now, before we start anything, I want to make sure you have set up your Go development environment. If not, now is the right time. [Follow this link if you haven't already.](https://dev.to/neel229_93/setting-up-dev-environment-for-golang-ipo)

**In your src directory create a directory for example called youngling in which we will store all the codes for beginner level concepts and projects.**

### Problem #0: The Good Ol’ Hello World

*In this introductory blog, we will look at how to define variables, and how to print out some text back to the user.*

### **Step 1) Declaring the package name**

The first thing in every Go program that we do is defining the package name. We will define the package name as **package main **here.

![Every Go program starts by defining the package name](https://cdn-images-1.medium.com/max/2000/1*qVqj0g8OGY3YjImzRrHNKw.png)*Every Go program starts by defining the package name*

### **Step 2)** Importing the required packages

After we have defined the package name, we will always write the package imports that we will use in our program. Think of packages as pre-written codes to perform a specific action. In our case, we have to print out a text to the user and for that, we will import a package called “**fmt**” which stands for format. More on that later.

![We always enclose the package name which we are importing inside double-quotes. The red squiggly shown above is because we have imported the fmt package but haven’t used it yet.](https://cdn-images-1.medium.com/max/2000/1*DiuC7g9rr_LP043zcAT2CQ.png)*We always enclose the package name which we are importing inside double-quotes. The red squiggly shown above is because we have imported the fmt package but haven’t used it yet.*

### Step 3) Main function block

Now, since we have imported the **fmt** package, we will now move on to write the main logic behind solving the problem. The main driving code of our problem is always written between the **main function**. Think of it as the entry point of our code which runs as soon as we first compile our program.

![The syntax to declare the main function is shown in the above picture.](https://cdn-images-1.medium.com/max/2000/1*e4bzcP1H8OuOACTg9_j-LA.png)*The syntax to declare the main function is shown in the above picture.*

Now, we will work on writing the logic which will solve our problem, but remember that we will have to write it between the main function block otherwise it won’t run and will result in an error.

### Step 4) Declaring the variable

There are various ways to declare a variable in go but we will focus right now on only one. More on that later. We will create a variable named **input **which will store the text which we want to print out to the user.

![](https://cdn-images-1.medium.com/max/2000/1*eIjtYCbopriqMa3ncR_goQ.png)

We define a variable by writing the ***keyword*** **var **followed by the **variable name **and the **type **we want our variable to be. *(A keyword is a text which has a special meaning attached to it and cannot be used by us in our code. Golang has 25 keywords which we will discuss further down the line.)*

In this example, we will use string as the type of input. [Follow this link](https://golang.org/ref/spec#Numeric_types) to see the various types Golang offers us. We will cover each type and will also define our own types later on.

### Step 5) Assigning value to our input

In Golang, we cannot leave unused variables, meaning we have to use every variable we define in our code. To use a variable, we have to do two things, first **declare and assign values to it**, and secondly **perform some actions on it.** We will now assign value to our variable input and print it out to the user. I am assigning the value “Hello World” here but you can write anything you want but between the double-quotes.

![Assigning value Hello World to the variable input.](https://cdn-images-1.medium.com/max/2154/1*BcCvrZAne9-QC3DXTKL-Gg.png)*Assigning value Hello World to the variable input.*

### Step 6) Printing the value

Now, since we have assigned the value to our variable, “*Hello World*” in my case, we will try to print it. For this, **we will use a method called Println from the package fmt**.

![](https://cdn-images-1.medium.com/max/2000/1*6EEIc1EVBR2lxYwZe6_JXg.png)

We use the Println method as shown above. First, we type the package name from which we are using the method, followed by a **. (dot)** and the method name. While writing the code, you can repeat this in your head, “**From package fmt, I am using the Println method**”.

Note: All the methods are called only by placing parenthesis in the end and passing inside the variable we want to carry operations on.

    (input)

Now save the file as main.go. See here, we have an extension of .go with our filename. **All go programs are saved as <filename>.go**

### Step 7) Run the program

We have written all the code required to print out the text to the user. Now to run the code, we have to first compile it into the byte code which will tell our computer to do the intended operations. For that open your terminal inside the code directory and run the following command.

    go run <filename>.go 

    For example: 

    go run 01.go

You should see the results in your terminal.

![](https://cdn-images-1.medium.com/max/2000/1*JAfRLaYmlW6ckNm5KAOb1Q.png)

---

### So that’s it for this blog. We have covered the extreme basics of how we start writing our code. We will learn more about packages, methods, and the different ways of declaring and assigning variables in the later blogs. Till then, adios.