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.
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.
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
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.
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.
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.
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 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.
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.
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.
Now save the file as main.go. See here, we have an extension of .go with our filename. All go programs are saved as .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.
Top comments (2)
So, Is it going to be updated or what? Where were the real-world problems :D
Well this post ain't going to be updated but in the later posts like after the users following the series have firm grasp of topics, we will solve problems like gpa calculator cli app using cobra-cli,
champions league live score update using websockets, etc.