What is Google Go?

Google Go is an open-source programming language designed at Google by Robert Griesmer, Rob Pike, and Ken Thompson. Go is syntactically similar to C language; in addition to memory safety, garbage collection, structural typing and CSP style concurrency. It has a rich standard library. It initially developed in the year 2007 and launched in November 2009. It is updated recently on April 11, 2019.

Google Go is also termed as Golang. Golang is the implementation of assembly language (GC) and C++` (gccgo).

Prerequisites

For Go programming language, one should be familiar with C programming. If you are good at C, then it would be easy for you to understand the concept of Go programming. I have tried to make this tutorial easy and start it from scratch level.

Features of Go programming

Some of its essential features are as follows-

  • Fast compilation
  • Inbuilt concurrency support
  • It supports interfaces and type embedding
  • It is modern fast and powerful.

Some of its features are excluded intentionally to keep it concise and straightforward.

Some of the Go’s  excluded Features are-

  • Type inheritance support
  • The method or operator overloading
  • Pointer arithmetic
  • Assertions support
  • Generic programming support

Structure of Go programs

The basic structure of Go programs contains the following parts-

  • Declaration packages
  • Import packages
  • Variables
  • Statement and Expression
  • Function
  • Comments

A Beginners Guide to Go installation Step by Step

Go programming can be installed on a different operating system like Windows, Linux, Mac, etc. For installing Go language, go to its official website golang.org. Select desired package for Operating system like for windows 7, select the setup available for window7, and follow the installation process.

Setting environment variable (for windows)

You can find the setting for environmental variables on the “advanced” tab of the “system” control panel.

Check Go version- You can check the version of the Go language by using go version command.

First Go program

We can write Go code on any basic editor in any desired directory and save it with extension .go

Save it with the extension .go. I saved it as First.go

Execution- open command prompt and type command-

go run First.go

Output-

Have a look at the following image.

 Go Hello world

Go package, import, and visibility

Packages-

Our program for easy maintenance categorizes packages. Every file of Go program belongs to some packages. Each Go program must have “main” package.

Package’s name should be written in lowercase letters. If a package is changed and recompiled, all the client’s programs belong to that package must be recompiled too.

Import-

Import is used to import any package from the library. A Go program is linked to the different package through the import keyword.

We can import multiple packages by a single statement, Like-

We can use one more way-

Visibility-

The identifier can be a variable, function, type or struct field or constant. The identifier can be declared as in the lower case or upper case.

If the identifier declared in lowercase, then it will be visible inside the package. If declared in uppercase, then it will be visible inside or outside the package which is called export. A dot (.) operator is used to access the identifier: Eg. pack.Date, Here pack, is the package name and date is the identifier.

Google Go Data types

Data types represent the type of value stored in a variable, type of the value a function returns, etc.

There are three basic data types in Go-

  • Numeric type
  • String type
  • Boolean type

Numeric type-

It represents various numeric values like integer, floating point, and complex values.

Some of its types are-

  • In8 (8bit signed integer)
  • Int16 (16bit signed integer)
  • Int32 (32bit signed integer)
  • Int64 (64 bit signed integer)
  • Uint8 (8bit unsigned integer)
  • Uint16 (16bit unsigned integer)
  • Uint32 (32bit unsigned integer)
  • Uint64 (64bit unsigned integer)
  • Float32 (32bit floating point numbers)
  • Float64 (64bit floating point numbers)
  • Complex64 (Has float 32 real and imaginary part)
  • Complex128 (Has float 32 real and imaginary part)

String-

A String represents a sequence of characters (bytes). Various operations can be performed in a string like concatenation, extracting substring, etc.

Boolean types-

It represents only two values either true or false.

Variables-

Variables refer to a memory location for storing some value. The type parameter represents the value type that can be stored in the memory.

Syntax-

Once a variable is declared then you can assign the variable type value. You can also give an initial value at the time of declaration of the variable.

Multiple variables can also be declared in a single statement-

A Go program for understanding variables-

Output-

Constant Variables-

Constant Variable is declared by using the keyword “const”.  Value of constant variables cannot be changed if once assigned.

Example-

Output-

Loops in Go-

Loops are used to execute a block of code repeatedly according to the given condition. Most programming languages support three types of loop, for, while and do while.

Note: Go supports only for loops.

Syntax-

The initialization_expression is executed first then the evaluation_expression is evaluated. If it’s true, then block code is executed. The itereation_expression id is executed, and the loop starts again. This process will continue until the evaluation_expression becomes false.

Program for loop-

Output-

Conditional statement-

The if-else statement-

It is a conditional statement.

Syntax

Go program for checking the given number is positive or negative-

The Switch statement-

The switch is also a conditional statement. Switch statement compares the conditions and evaluates a result.  Once a match is found, the statement associated with that case is executed. We can also add a default case to switch which will be executed if no other matches are found.

Syntax-

Program for switch case-

Output-    

The Goto statement-

The Goto is a jump statement. It is used to transfer the control to other parts of program. Every Goto statement has a label, we use that label to transfer the control of the program.

Example-

Output-

The break statement in Go-

The break statement can be used in for loop for the counter, condition etc. It can also be used in the switch statement. A break statement is used to break out of the innermost statement in which it occurs.

Syntax-

Example of break statement-

Output-

Arrays –

An Array is a collection of elements which have the same types of data. The Array represents a fixed size and named a sequence of elements. An array cannot have an element with two different data type. We can’t change the size of an array if once defined.

Each array element can be assigned value as-

Index of array starts from size 0 to 1

We can assign the values to array at the declaration time-

Example for Go array-

Output-

Slice in Go-

A slice is a segment of an array, or a partial view of an underlying array to which it points. You can access the slice name and index number just as in an array. We can change the size of the slice, but we cannot change the size of an array.  Contents of the slice are actually the pointers of the array. If we change the elements in slice then it also affects the contents of the array.

Syntax-

This will create a slice name from an array with elements at the index start to end -1.

Pin It on Pinterest

Share This