In this article, we’ll be discussing a new and widely popular language – “Golang” We’ll be covering the following topics –
Introduction Installation Hello World in Go Resources Conclusion
Introduction
Golang, also known as Go, is an open-source programming language created by Google in 2007. Since its invention, Go has been growing immensely over the years. Golang has a minimal syntax quite similar to other scripting languages. It was built to improve the productivity of the developers allowing them to write programs in a better way.
Why Golang?
Clean, concise and efficient Minimal and easy to learn Built for speed (Used by companies like Google, Uber, etc) Simple binaries used on the server Supports Garbage collection Golang is one of the most loved languages
Installation
Go is an open-source and free programming language. You can either install it via compiled binaries or build it from the source. Today, we will be installing it via the binaries available.
Head over to Golang official website
Click the Download Go button and choose the binary based on your system – Windows Linux Mac I would recommend you to download the stable version, at the time I write – Go v1.14 is stable. You can also install Go from source, follow the instructions here
You can check if Go is installed successfully by running the following command – In case you have some doubts, All the download and installation related information is available at golang.org.
Hello World in Go
Now that we have setup Golang, we are ready to write the simplest and most famous “Hello World” program.
Let’s create a simple helloworld.go file. Yes, you guessed it right golang has a .go file extension. Now, open up your favorite editor and copy-paste the following code.
Save the file and head over to the terminal. Before some explanation. Let’s run your first Go program! In the terminal (ensure you are in the same directory where you created the helloworld.go file)
Tada! You will see the output like this – Congratulations! You just ran your first go program successfully. Let’s understand this basic code now –
package main – package is a keyword in Golang that defines a directory to which your file belongs. There can be only one package inside a folder. You can create multiple packages by creating multiple directories. Package is used to identify the path to your methods and variables. Here, main is a special package that is used when we are writing executable programs. import “fmt” – import is again a keyword which fetches external and internal packages. Here import fetches the “fmt” package from the go library. You can use import keyword to include any package. This lets us access the Println function which is predefined in the “fmt” package. You can even check the code here. Then, we define our main function that gets executed automatically when we run. main() is a special type of function and it is the entry point of the executable programs. So, when we run our code – this function gets executed.In main() we simply call Println(arg)which prints the string passed to it on the terminal.
Resources
Learn Go basics in the browser – “Tour of Go” Little go book for book lovers Choose your favorite editor
Conclusion
In this tutorial, you got an introduction to Golang and how to get started with it. We installed Go on our system and ran a “Hello World” program. The next steps from here would be to learn some basics around variables, looping, and functions in Golang. You can even try running programs in the browser at play.golang.org. Hope you learned something new!