What is Go?
Go is a statically typed, open-source, compiled programming language. We are known for greater efficiency in speed, performance, and usability.
The above brief introduction may not be straightforward. So let's break down the definition a bit -
First, let's talk about “statically typed”. Statically typed means that the data type of all variables used in Go is determined at compile time before runtime. That is, the data type is determined before the program is run.
Then comes, “Open Source”. Due to its open-source nature, anyone can create packages for any task, and since Go is a package-based language, Go's community support can contribute directly.
"Compiled" means that all Go code is directly converted to machine code and can be run directly on a computer's processor. Go performs better than other interpreted languages (JavaScript, Python) for this feature.
Usage of Go
For building any web application, web servers, APIs, and today's most popular microservices architecture.
For system software, such as operating systems, device drivers, and network protocols.
Go has its own concurrency support for distributed systems, data pipelining, and streaming applications.
For network applications like servers, proxies, and load balancers.
For various DevOps and cloud applications.
Go Install
We chose VS code as the IDE to start our Go journey, as it is one of the most widely used IDEs and is open source.
The process of installing Go is slightly different depending on the platform. For now, we are only providing guidelines for installing Go on Windows.
Install Go on Windows –
First, download the .msi file for Windows from this link.
Once the file is downloaded, open it and follow the prompts-
- First, after waiting for a while, I will click on the Next button.
- Then click on the Next button again.
We can choose any directory we want, but now we will select the default directory and click Next button again.
If you click on the Install button, Windows administrative will ask for permission, then click on the Yes button.
When the installation is finished, we will click on the Finish button.
To check whether it is installed correctly, open the command prompt and run the go version command, then we will see something like the image. Congratulations, Go has been successfully installed on your system.
Install Go on Linux –
First of all, I will download the Linux binary release file by going to this link.
If any version of Go is already installed on the system then remove it from the system by running the following command –
sudo rm -rf /usr/local/go
- Then extract the downloaded file to /usr/local location –
sudo tar -C /usr/local -xzf gox.x.x.linux-amd64.tar.gz
- Add /usr/local/Go/bin to PATH environment variable –
export PATH=$PATH:/usr/local/go/bin
Log-out once and log in again.
By opening the terminal and running the following command, if we see the version of Go, then we will understand that our installation process has been completed properly –
go version
The first program in Go -
When starting to learn a language, our first task is to run a program before we learn something well, with which we can say "Hello World!" to the terminal. I can print the output. Then, without delay, fulfill that responsibility -
- First, create a directory and open it with vs code –
> mkdir hello-world
> cd hello-world
> code .
main.go
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
The entry point of any Go program is its main() function. That is, a Go program must have func main().
Inside the main() function, Println is a function of the fmt package. Go is a package based language. Since we have used the fmt package here, we have to import it. And by using this Println() function we can print “Hello World!” in the terminal.
To run the above program, we will run the following command from the terminal –
go run main.go
This will build and run the program together and we will see “Hello World!” in the terminal. I will get the output.
If we want to save the program as an executable binary only, then run the following command –
go build main.go