Let’s Go ~ A Complete Guide (Part ~ III)

Himanshu Singh
MindOrks
Published in
5 min readAug 9, 2018

This is the third part of the series Let’s Go ~ A Complete Guide.If you have not followed the Part ~ I and Part~II, I would request you to go and get an idea why should we learn GoLang.Following is the content of the Series,

  1. Introduction to Go
  2. Go Environment Setup
  3. Go Basics
  4. Control Statement and Functions
  5. Structs
  6. Object Oriented Programming in Go
  7. Concurrency

Every Programming language is built with some sort of building blocks like variables, constants etc. We will be discussing about it in this part.

Variable

  • Variables are like containers to store some value. To understand this better we can call a variable as a bottle which stores water, juice etc.
  • Constant are like containers whose value doesn’t change and are fixed.

In Go, we declare variables using var keyword. Variables are used to store some values in it. It may be String, Integer, Float.

We can also declare variables without defining a data-type,like

var variableName = value

Example:

// Here variable rollNumber is declared without any specific //datatype but stores a type integervar rollNumber = 44

or without using the keyword var.But We use := to assign values

variableName := value

Example:

// Here variable topic is declared without var keyword and it is of // datatype string as it stores the value of type string  topic := "Android"

Facts about variables,

  • := can only be used inside a function.
  • var is used to define global variables.
  • _ is a specific type of variable. Any value assigned to it like _ := 12 will be ignored.
  • If a variable declared in a program is not used, will throw an error.
  • A global variable is a variable with global scope, meaning that it is accessible throughout the program from any point

Constants are declared exactly same as variables, but its values can’t be changed at the runtime.

const variableName = value

Example:

// Here cosntant holds a constant value of type string and it wont change at run-timeconst topicName= "Android"

Basic Data Types

Boolean

  • In Go, we use bool as boolean datatype having values as true or false .
  • Default values is false

Example

Example for Boolean Variable

Numerical Types

  • In Go, we have int(integer), uint(un-signed integer).
  • Their length depends upon the bit of operating systems.
  • Float also has float32 and float64 types with default being is float64
  • Go also has complex64 and complex128 as data types with with a 32-bit real and 32-bit imaginary part and with a 64-bit real and 64-bit imaginary part respectively.
//Real + Imaginary(i)
var complexNumber complex64 = 5+3i

Example

String and Error Type

  • Strings are represented by double quotes "" or backticks ``
  • We use the + operator to concatenate two strings.
  • Error are something which gets generated when the desired value/state is not accepted.
  • Go has error type for purpose of dealing with error messages. There is also a package called errors to handle errors using,
variableName := errors.New("Error")

Example

Amazing Declarations of Variables

  • We can group the declaration of the variables and constants.
  • Any variable/Function/Constants that begins with a capital letter can be exported, private otherwise.

Array

  • Go has two type of data structure which can handle the list of data
  • Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type.
  • An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
  • Default value of all element in an array is 0
  • Array has fixed lenght
  • Here, in array we don’t have to device all variables individually
var arrayName = [size]dataType
  • Arrays are always 0 based i.e. the first element holds the 0th position, second element holds the 1st position and so on.
  • to access any element in array we use , arrayName[position] . For example, if we have to use 1st element in the array we will use arrayName[0]
  • It is also possible to declare the array as
  • We can also declare, two-dimensional Arrays,
arrayName := [n][m]{{array1},{array2}}
  • Here, array1 and array2 should have m elements
  • n is the number of arrays or the size of array arrayName

Example

arrayExample.go

Slice

  • Slice are are the array in Go but with Dynamic Size
  • We don’t have to declare the size explicitly.
  • Slices and Arrays must be defined with a data type and has same type of elements
  • it is an array which can grow or shrink in size based on the requirement.
  • Syntax :~
var sliceName [] dataType//orsliceName := [] dataType {n1,n2,n3}
  • Slice are also always 0 based i.e. the first element holds the 0th position, second element holds the 1st position and so on.
  • Here n1,n2 are the default values at 0th, 1st Position.
  • Slice can also be formed from existing Slice/Array using
arrayName[i:j]
  • Here, i is start Index and j is the end index
  • If i is omitted and written as arrayName[:j] then it starts form 0th Index and if j is omitted then it ends with the last element of array
  • So now you must have understood , to use the whole array as slice we use
arrayName[:]

Functions for slice

  • len calculates the length for slice
  • cap gets the max length of slice
  • append adds more elements to slice
  • copy copies from one slice to other and forms a new slice.

Example

Maps

  • Map are key value pair.
  • Each value have a unique key,
  • Map is useful if you have to search, update or delete elements on the basis of key.
  • It is declared as,
var mapName map[dataType 1] dataType 2
  • Here, dataType 1 is the data type for Key and dataType 2 is data type for Value.
  • Map doesn’t have a fixed length
  • Map are not accessible via index. To access Map we used Key.
  • We use delete to delete elements in Map.
delete(mapName,keyName)

Example

mapExample.go

Make, New

  • make does memory allocation to models like map and slice
  • new allocates memory to type
  • make returns non-zero values.
  • new returns pointers.
  • make can be used for slice, map, and channel, and returns a type T with an initial value.
  • Example,
mapName := make(map[string]int)

Let’s move to , Part ~ IV, to understand more about the function, loops and control statements. See you there !

If you are liking this series, add 👏 and show some love. 💌

Follow me on Facebook, Twitter, Instagram 😃

--

--