An Interest In:
Web News this Week
- November 21, 2020
- November 20, 2020
- November 19, 2020
- November 18, 2020
- November 17, 2020
- November 16, 2020
- November 15, 2020
Some of Our Sources
- Joshua Blankenship
- Smashing Magazine
- Naldz Graphics
- Line 25
- CSS Tricks
Learn Golang by Solving Real-World Problems
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.
In this introductory blog, we will look at how to define variables, and how to print out some text back to the user.
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.
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.
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.
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 wont run and will result in an error.
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.
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.
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
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.
Original Link: https://dev.to/neel229/learn-golang-by-solving-real-world-problems-2b2o