Go Tutorial - Section 1

I am beginning this blog with an assumption that you have already read my previous blog about the basic concepts of Go. If not, jump over here to understand the basic concept and the reasons why Go is seeing such a huge spike in its usage. The tutorials will be divided into multiple sections with each section covering a certain list of topics in them. Once we have covered all the topics we will move towards building a REST API using Go. The topics covered in this section:

  1. Environment Setup

  2. Go Structure

  3. Basic Syntax

  4. Data Types

Environment Setup

Unix based system:

Download the archive and extract it into /usr/local, creating a Go tree in /usr/local/go. For example:

tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gz

Choose the archive file appropriate for your installation. For instance, if you are installing Go version 1.2.1 for 64-bit x86 on Linux, the archive you want is called go1.2.1.linux-amd64.tar.gz. (Typically these commands must be run as root or through sudo.) Add /usr/local/go/bin to the PATH environment variable. You can do this by adding this line to your /etc/profile (for a system-wide installation) or $HOME/.profile:

export PATH=$PATH:/usr/local/go/bin

Note: Changes made to a profile file may not apply until the next time you log into your computer. To apply the changes immediately, just run the shell commands directly or execute them from the profile using a command such as source $HOME/.profile.


The Go project provides two installation options for Windows users (besides installing from source): a zip archive that requires you to set some environment variables and an MSI installer that configures your installation automatically.

  • MSI installer Open the MSI file and follow the prompts to install the Go tools. By default, the installer puts the Go distribution in c:\Go. The installer should put the c:\Go\bin directory in your PATH environment variable. You may need to restart any open command prompts for the change to take effect.

  • Zip archive Download the zip file and extract it into the directory of your choice (we suggest c:\Go). Add the bin subdirectory of your Go root (for example, c:\Go\bin) to your PATH environment variable. Setting environment variables under Windows: Under Windows, you may set environment variables through the "Environment Variables" button on the "Advanced" tab of the "System" control panel. Some versions of Windows provide this control panel through the "Advanced System Settings" option inside the "System" control panel.

Test your installation:

Check that Go is installed correctly by building a simple program, as follows. Create a file named hello.go that looks like:

package main
import "fmt"

func main() {
	fmt.Printf("hello, world\n")

Then build it with the go tool:

$ go build hello.go

The command above will build an executable named hello in the current directory alongside your source code. Execute it to see the greeting:

$ ./hello
hello, world

If you see the "hello, world" message then your Go installation is working.

Go Structure

A Go program basically consists of the following parts:

  • Package Declaration

  • Import Packages

  • Functions

  • Variables

  • Statements and Expressions

  • Comments

Let us have a look at the Hello World Program again.

package main

import "fmt"

func main() {
   /* This is my first sample program. */
   fmt.Println("Hello, World!")

Let us take a look at the various parts of the above program −a

  • The first line of the program package main defines the package name in which this program should lie. It is a mandatory statement, as Go programs run in packages. The main package is the starting point to run the program. Each package has a path and name associated with it.

  • The next line import "fmt" is a preprocessor command which tells the Go compiler to include the files lying in the package fmt.

  • The next line func main() is the main function where the program execution begins.

  • The next line /*...*/ is ignored by the compiler and it is there to add comments in the program. Comments are also represented using //.

  • The next line fmt.Println(...) is another function available in Go which causes the message "Hello, World!" to be displayed on the screen. Here fmt package has exported Println method which is used to display the message on the screen.

  • Notice the capital P of Println method. In Go language, a name is exported if it starts with capital letter. Exported means the function or variable/constant is accessible to the importer of the respective package.

Basic Syntax

Now that we know the structure of a Go Program, let us look into its building blocks.

A Go program consists of various tokens. A token is either a keyword, an identifier, a constant, a string literal, or a symbol. A Go identifier is a name used to identify a variable, function, or any other user-defined item. An identifier starts with a letter A to Z or a to z or an underscore _ followed by zero or more letters, underscores, and digits (0 to 9).

identifier = letter { letter | unicode_digit }.

Go does not allow punctuation characters such as @, $, and % within identifiers. Go is a case-sensitive programming language. Thus, Aman and aman are two different identifiers in Go. Just like any other language, Go has its own set of reserved keywords that cannot be used as identifiers. Below is the list of reserved keywords in Go.

Just like Python, Go doesn't need a semicolon to indicate the end of a statement. In a Go program, the line separator key is a statement terminator. The Go compiler internally places “;” as the statement terminator to indicate the end of one logical entity.

Data Types

Data types specify the type of data that a valid Go variable can hold. In Go language, the type is divided into four categories which are as follows:

  1. Basic type: Numbers, strings, and booleans come under this category.

  2. Aggregate type: Array and structs come under this category.

  3. Reference type: Pointers, slices, maps, functions, and channels come under this category.

  4. Interface type

Here, we will discuss Basic Data Types in the Go language. The Basic Data Types are further categorized into three subcategories which are:

  • Numbers

  • Booleans

  • Strings


In Go, Numbers are divided into 3 subcategories

  1. Integers: In Go language, both signed and unsigned integers are available in four different sizes as shown in the below table. The signed int is represented by int and the unsigned integer is represented by uint.

2. Floating-Point Numbers:

In Go, floating-point numbers are divided into two categories. a. float32, which represents 32-bit floating numbers.

b. float64, which represents 64-bit floating numbers.

3. Complex Numbers:

In Go, a complex number is divided into two parts, each part of which can either be a float32 or float64. Inbuilt functions help us create complex numbers. Complex numbers can be of two types:

a. complex64 - contains float32 as its real and imaginary parts.

b. complex128 - contains float64 as its real and imaginary parts.


The string data type represents a sequence of Unicode code points. Or in other words, we can say a string is a sequence of immutable bytes, means once a string is created you cannot change that string. A string may contain arbitrary data, including bytes with zero value in the human-readable form.

// Go program to illustrate 
// the use of strings 
package main 
import "fmt"

func main() { 
	// str variable which stores strings 
str := "AmanSinha"
// Display the length of the string 
fmt.Printf("Length of the string is:%d", 
// Display the string 
fmt.Printf("\nString is: %s", str) 
// Display the type of str variable 
fmt.Printf("\nType of str is: %T", str) 


Length of the string is:9
String is: AmanSinha
Type of str is string


The boolean data type represents only one bit of information either true or false. The values of type boolean are not converted implicitly or explicitly to any other type.

// Go program to illustrate 
// the use of booleans 
package main 
import "fmt"

func main() { 
	// variables 
str1 := "AmanSinha"
str2:= "amanSinha"
str3:= "AmanSinha"
result1:= str1 == str2 
result2:= str1 == str3 
// Display the result 
fmt.Println( result1) 
fmt.Println( result2) 
// Display the type of 
// result1 and result2 
fmt.Printf("The type of result1 is %T and "+ 
				"the type of result2 is %T", 
							result1, result2) 


The type of result1 is bool and the type of result2 is bool

The Section-1 of Go Tutorials concludes here. In the next section, we will continue with variables, constants, operators and decision making.



©2020 by Aman Sinha.