Golang has been there for a decade and has been improved a lot since its beginning. Now, it is one of the most simple and popular programming languages which makes many beginners and experienced people of other programming languages switch their language to Go. Here, we provide a golang for beginners tutorial as to how to develop in Go and after you completely read this, you would be able to set up the environment, understand basic sections of a Go file and compile and run your programs

1. Getting started

Before you can do anything with Go, you got to install the environment into your system and make sure everything is set properly. So, let us start with the first step of installation of Go:

2. Installing and Setting up the environment

Setting up a Go runtime environment is very simple actually. Mac and Windows users just need to open the package or the installer provided and follow the prompts to completion. For Linux though, there are no automatic installers. After you download the archive, extract it to /usr/local. Then, you need to add /usr/local/go/bin directory to your path variable.

After you perform installation, you need to restart the command prompt or terminal to use it.

3. Basics of writing programs in GO

There are only 25 keywords in Golang through the combination of which you would write programs. However, before going to write programs, you need to understand the basic structure of a Go Program.

A General Go program would contain the following parts:

Declaration of the package, which specifies the package name in which your program would lie. Using packages is mandatory in Go lang as it groups programs and executes them based on packages.

Import Section, Where you would be importing all the required packages you would use in the current program.

Functions, Variables, Statements, and Expressions where you detail the logic of your program.

Comments, which specify the reason and some other information about your code which are ignored by the compiler.

With that said, let us write the first program you would write in any programming language — Hello World!

4. Writing a simple Hello World program

All the Golang source files would have a file extension of the go. Before writing the Hello world program, open your favorite text editor, create and save the Hello.go file. You may create folders for organizational purposes.

Have a look at the Hello World Code in Go:

package main

import “fmt”

func main() {
fmt.Println(“Hello, World!”)
}

Output: Hello, World!

As we have talked about earlier, it contains the sections of the package, imports, main, and a statement.
Package main signifies that it belongs to the main package while the import “fmt” imports the fmt package which contains many functions for formatting text and also printing to the console.

As we require a printing function to print Hello World, we choose an import fmt package. However, how would you know what package is required for a particular application? This is where documentation comes. All the packages for Go are well documented and from documentation, you can learn the contents of all the packages.

Function main signifies the starting point of your program which is mandatory for any go application.

After you write the code, save your file and you are ready for execution!

5. Executing the program

You have two choices for execution, you can directly run it or just build(compile) it and run it later.

To run it directly, open your cmd or terminal and after navigating to the directory your program is in, type go run and then give your file name as a parameter. The Result should be displayed on the console!

Building works similarly and instead of using the go run command, you would use the go build command which generates an executable file.

That completes our golang for beginners tutorial on Golang. This is just a starting point and there are lots to explore and create with Golang!