Getting Started with Go Programming Language

Eze Oluchukwu

Getting Started with Go Programming Language

tech
tutorial
tips

Go is less complicated than some programming languages; you do not need to stress yourself to get the code flawlessly, which could save you some time. It is excellent and one of the best programming languages you need to know. However, to use it for development, you must understand how it works. In this article, you will realize what Go is and the areas you can apply it. You will also understand how to install it for different devices and create a Go program, learning the various parts and structures.

Overview of Go Programming Language

Go, also known as Golang, is a statically typed, cross-platform, compiled, open-source programming language designed by Google. It helps create several kinds of applications, but it is famous for building scalable web servers or systems in general. With Go, you can build fast and high-performance applications. Three Google engineers, Robert Griesemer, Rob Pike, and Ken Thompson, developed Go in 2007. C++, Java, and Python inspired these engineers to develop Go. However, they needed better performance and a simple language, so they created it.

Features of Go

Go has many features and includes the following:

  • The syntax is similar to C++.

  • Built-in concurrency support is available through lightweight processes, channels, and select statements.

  • It has an automatic garbage collector.

  • There is a fast compilation time.

  • There is support for environment-adopting patterns, which are similar to dynamic languages.

  • It supports Interfaces and Type embedding.

  • You can create statically linked native binaries without external dependencies.

Applications of Go

There are different areas where we can apply Go and build various things. Some of them include the following:

  • Web development (server-side)

  • Develop network-based programs: Building network servers and clients.

  • Develop cross-platform enterprise applications

  • DevOps and Cloud: Popular for building CLI tools, Kubernetes, Docker, and other cloud-native applications.

  • Microservices: Due to its concurrency model and performance

Installing Go

Different devices have different ways of installing Go. Look for the one that fits your operating system and download it. In this tutorial, we will demonstrate how to download and install Go for Windows. 

On Windows

To get Go running on Windows, you must first download it, open the file, and then install it. 

Here is how you can check whether you installed Go correctly:

  • Open your windows, and click on the search menu.

  • Type in cmd and press "Enter."

  • The command prompt window will appear.

  • Now type the command:

go version
  • Then press "Enter."

  • This will show you the version of Go that has been installed.

On Macs

For Mac users, you can find the rundown for downloading and installing Go for Mac OS.

On Linux

Find the rundown on downloading and installing Go for Linux.

Creating a Go Program

To get started with Go, you need two software:

  • A text editor: To start programming with Go, you need a text editor. This will enable you to write your code. An integrated development environment (IDE) will also provide extra features like debugging and code compilation. Some IDEs you can use include VS code, notepad, vim or vi, Eclipse, etc. You can also use Web-based IDEs, but their functionality is limited.

  • A compiler: The source code is what humans can read and needs to be converted to a language the computer can understand. The compiler is the software that translates the source code into an understandable computer language. In addition, it is what allows us to build, compile, and execute Go code.

Text Editor Installation 

First, let us select a text editor. 

You can use any text editor. However, I recommend VS Code because it has excellent integration with Go and can make your work easier.

Let us see how to access VS code and use it as our text editor:

Go Compiler Installation

There are different ways to use the Go compiler, either as an online Go compiler through Go Playground or locally on your computer.

For this tutorial, we will use the Go compiler locally on our computer. To set up VS Code to work with Go, we will install an extension to enable Go's support in VS Code. 

To access the Go compiler using the Go support in VS code:

  • Click on the extension icon on the left side of the VS code.

  • You can also press "Ctrl + Shift + X on Windows."

Or you click "settings" and click "extension."

  • Once you have opened the extension, search for Go in the search box.

  • Now, go ahead and install the Go extension by clicking "install."

Go extension

  • The Go extension requires additional tools to analyze and manage your Go code effectively.

  • Once you have installed the Go extension, you need to open the command palette to add the extra tools.

  • To open the command palette, click on the "View" menu, then select "Command palette" or press "Ctrl + Shift + p."

  • In the Command Palette, type and select Go: Install/Update Tools.

  • A list of tools will appear. Select all the tools and then click "OK." The tools will be installed or updated.

You can now use Go on your VS code!

Creating Your First Go Program

To create a Go program, you will create files with your text editor, known as Source files. The source files contain the program code and have the extension ".go."

Let's create a Go program that prints the word "Hello World!!!!!!" This program will help us learn how to develop programs on Go. 

Let us take these steps to create the program:

  • Create a new file on VS code or text editor of your choice, with the name "My-First-Go-Program" or any name you want.

  • Add the extension ".go," which is "My-First-Go-Program.go"

  • Open the file on VS code.

  • Then, copy and paste the code below into it.

package main

import "fmt"

func main() {

 fmt.Println("Hello World!!!!!!")

}

  • Now, run the code by clicking on the "View" menu.

  • Then select "Terminal."

  • Now run the program using the command below:

go run My-First-Go-Program.go
  • Any name you saved your file with is what you will use above.

  • It will print "Hello World!!!!!!" on the terminal, as seen in the output below:

Yay! You have written your first Go program.

Basic Structure of the Go Program

You might be wondering what actually happened in the code you pasted into the file. Worry no more because, in this section, we will go over the fundamental structures of a Go program and explain why you see what you see in the code.

A Go file comprises the following parts:

  • Package Declaration

  • Import Packages

  • Functions

  • Words and Expressions

To understand how to create programs properly on Go, we will use the above "Hello world" program example as a reference. Here is an explanation of the different parts of the Go program:

* Every Go program has a package declaration as it runs in packages. A package can be equated to a workspace or project. In addition, it has many files that end with the extension ".go." Notice that the first line of the program is package main and shows the name of the program's package. The main package in a Go program helps to make an executable package and indicates the starting point of running the program.

* The second line of the example shows import "fmt". This preprocessor command gives access to all the code and functionality in the package fmt to the package main. The import statement is used to include packages that provide pre-written code we can use. Fmt is the short form of format, and it is the name of a standard library package in the Go language by default. It contains functions for formatting and printing text. In this case, we're using it to print a string to the console.

* The third line of the example is func main(). The main function is the entry point of a Go program. This import statement gives all packages access to code written in another package. In addition, it is the primary function to start the program execution and will execute any program in the curly bracket. Whenever we make an executable package in Go, it must have a function called "main."

* The fourth line is fmt.Println(...). It is another function in Go that makes the word you put in the bracket to be displayed or printed. Println stands for "print line," and it prints the text provided as an argument to the console, followed by a newline character. Hence, the message "Hello World!!!!!!" is displayed on the screen. The fmt package exported the Println method to display the message on the screen.

Note: The capital letter “P” of the Println method makes it possible to export a name in Go.

In summary, when you run this program, the Go compiler recognizes that this is the main package and imports the fmt package so that we can use its functions. The main function is then executed, printing "Hello World!!!!!!" to the console.

Conclusion

This article thoroughly explains the Go programming language, its applications, and how you can start using Go by installing the compiler and text editor. In addition, the different parts of a Go program have also been explained. Now, you can start using Go for your programming projects. Explore more and learn more about Go.