GO Language is also known as Golang. In this article, I'll explain basic information about Go Language. We will build a basic application to understand the theories of the Go Language.
Why Go Language?
Go is a programming language developed at Google in 2007. And it is open-sourced. But at that time there were many programming languages. But what is the purpose of this new programming language? In the last few years the infrastructure where the application deployed changed so many times. Multiple processors with Cloud infrastructure became pretty universal. So infrastructure became more scalable and dynamic. Most of the languages did not take full advantage of these infrastructure improvements. According to infrastructure improvements, we can do multiple tasks at once. That means Multi-Threading. This will make the applications faster and more user-friendly. In each thread, you have one task. And there may be thousands of threads that are running on parallel. But there are some issues working with multi-threading environments. Multiple threads refer the same resource at the same time will be an issue related to Multi-threading. This is called Concurrency. So the developers have a responsibility to handle such scenarios they need to write code to prevent conflicts when tasks run in parallel and accessing shared data. Many applications do have features for managing such scenarios. And there are some languages we can use to handle those multi-threading concepts by code itself. But the problem is that language code might be complex code or applications might be slow or those applications might be slow. That is the main difference and feature of the GO language.GO was designed to run on multiple cores and built to support concurrency. Concurrency in Go is cheap and easy.
Go language is used for writing highly performed applications and applications we run on scaled, distributed infrastructure with many servers typically on a Cloud Platform. As main characteristics of GO language, we can say that,
- Simple and readable syntax of a dynamically typed language like Python.
- Efficiency and safety of a lower-level statically typed language like C++.
GO is a combination of these 2 language characteristics.
GO used for Server-side or Backend-side?
Go can be used for both. For Micro-Services, Web Applications, and Database Services we can use GO language. Most of the Cloud-based Applications are written in the Go language.
Advantages of GO language
- Simple syntax
- Easy to Learn
- Read and write code
- Fast build time, start-up, and Running time
- Require fewer resources like CPU and RAM to run
- Source Code complies with machine code very fast and easily.
- Faster than interpreted languages, like Python.
- Consistent across different Operating Systems(You can deploy the same binary to different platforms)
Installation and Local Dev Environment
Let's start a simple project and try to understand the basic concepts of GO language. Before starting a project you just need 2 things.
- GO Compiler
- IDE for development
Here I am using GoLand IDE(IntelliJ IDEA) for development because it's pretty much easy to code. You can just install IDE from here: jetbrains.com/help/idea/quick-start-guide-g...
NOTE: If you are using GoLand IDE(IntelliJ IDEA) it will internally install the GO Compiler also. You don't need to install it separately.
If you're using another GUI you may need the installation. If you want to install Go Compiler separately use this: https://go.dev/doc/install After the installation, when you type Go it will show the list of Go commands.
Set up Project
Step 1: Open up your IDE and get a new terminal inside that. Now let's create a project folder and move to that newly created folder. Here I am going to create a simple Salon Appointment Managing Application.
NOTE: Here I am using Linux Operating System
$ mkdir salon-app
$ cd salon-app/
Step 2:
Let's create a new file called main.go Right click of the project folder and give File>New > Go File > Give a name as
Now you can get the newly created file. But How do we start writing code in Go? Let's try to write a simple Hello World Message into the console. For that first, we need to initialize our Go project. You need to run this command on your terminal.
$ go mod init <Your Project Name>
This will initialize your go application and it will automatically create a file called go.mod(This file consists of the name of the project and the version of GO that you are using in your machine).
Step 3:
In Go, everything is organized into packages. All our code must belong to a package. Now add package information to your main file. Add this code to your main file. package main
Step 4:
Now Let's add a print statement to our main.go file. But before that, we should give an entry point for GO. We have to declare an entry point of our application. Because when we run the GO application we need a main starting point for the execution(Where do we start the GO). The entry point is the main function that GO will look for whenever you execute your GO application. For that, we need to create the main function using "func" keyword. Now we can add our logic inside that main function.
Now go will know where to start your application. A program can only have 1 "main" function because you can only have 1 entry point.
Step 5:
Now we are going to add our print statement inside this main function. But we can't add a direct print statement here. Because print is coming from a built-in package called "fmt". We need to import that package also. Go programs are organized into packages. Go's standard library provides different core packages for us to use. "fmt" is one of these, which you can use by importing it.
import "fmt"
Now add print statement using fmt package.
func main() {
fmt.Println("Hello World")
}
Final Code here:
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}
You can get an idea of the packages and the functionalities using this link: https://pkg.go.dev/std FMT Package: https://pkg.go.dev/fmt@go1.17.7
Step 6:
Executing GO application. To execute your Go application use this command.
$ go run <Name of your file which you're executing>
Now you will get an output to the terminal like this. See it's pretty easy and fast.
Congratulations! You have completed a simple Hello World Application. I hope you will get a basic idea about GO Language, how to set up the environment, and write a simple application. We'll learn concepts and keywords in the next tutorial. See you again in another tutorial.
Thank You!