Advanced Data Types in Go


Introduction

In my previous blog, I discussed the basic data types in Go. In this blog, we will be going a step ahead and learning about advanced data types in GO.
We will be talking about

  • Arrays
  • Slices
  • Maps
  • Structs

Arrays

Arrays as in other traditional languages are a collection of variables of homogeneous types i.e means instead of creating 10 variables to store same type of data let's say marks we create an array that can store 10 elements.
Some of the key points about arrays in Go are:
  • Arrays in Go are fixed size
  • Array index starts from 0
  • Same type of data can be stored in an array
  • Data Type has to be given while the array declaration
The syntax for array declaration:

//Array declaration
var arr [3]int
//Indexing the array
arr[0]=1
arr[1]=2
arr[2]=3

We can also use shorthand notation for the Arrays

//Array declaration and initialization
arr1 := [3]int{1, 2, 3}


Slices

Slices, on the other hand, is a data structure more flexible than arrays. They are similar to arrays in the sense that they are used to store homogeneous data. They basically refer to the underlying array with some added flexibility.
A slice stores 3 things:
  1. Reference to the underlying array
  2. Length
  3. Capacity
A slice can be created in 2 ways:
  1. Using a slice operator on an array
  2. Creating a slice manually

Using Slice operator on an array

sl := arr1[0:2] // slice operator
fmt.Printf("%T\n", sl)

This will create a slice s1 which will be having elements arr1[0],arr1[1].
Elements of a slice can be accessed in the same way we access an array

fmt.Printf("%d\n", sl[0])

Creating a slice manually

Creating a slice is no different from creating an array, the only difference between creating an array is that we don't have to give the sizes.

sli := []int{1, 2, 3}

To get the length and capacity of this slice we can use len() and cap() functions

fmt.Printf("%d\n", len(sli))
fmt.Printf("%d\n", cap(sli))

 
Capacity of a slice is the maximum number of elements that can be stored in a slice, yes you heard it right a slice can be extended, that is one of the main difference between an array and a slice and the reason slices are extensively used in Go.

We can use inbuilt make function to create a slice with different capacity
sliMke := make([]int, 3, 5)
fmt.Printf("%d\n", len(sliMke))
fmt.Printf("%d\n", cap(sliMke))

Here we have created a slice that can store 3 elements but can be extended for up to 5 elements.


Maps

Maps is a data structure similar to hash maps in other languages. It is used to store key-value pairs. 

var m map[string]int

This creates a nil map in which no key can be added, this is just a nil map. As per official documentation 
"Map types are reference types, like pointers or slices, and so the value of m above is nil; it doesn't point to an initialized map. A nil map behaves like an empty map when reading, but attempts to write to a nil map will cause a runtime panic; don't do that."

To initialize a map we can use built-in function make

var m map[string]int
m = make(map[string]int)
m["a"] = 1
m["b"] = 2
fmt.Println(m)

We can access, delete and count the number of keys in a map using the following function

m = make(map[string]int)
m["a"] = 1
m["b"] = 2
fmt.Println(m)
fmt.Println(m["a"])
delete(m, "a")
fmt.Println(len(m))

We can also check if the key exists or not in a map using the accessor statement in the follwoing ways

v, ok := m["b"]
fmt.Println(v, ok)

the variable ok will be true if the key is present else false.

Structures

Structures are data structures to implement data encapsulation in Go. A structure is collection of heterogeneous data.

A structure can be declared as follows:

type person struct {
name string
age  int
}

This creates a structure name person with 2 variables
  1. name of type string
  2. age of type int
This will not create any variable just declaration of a struct to create a variable of type person and access its variable we need to initialize a variable of type person

p := person{name: "Japneet", age: 27}
fmt.Println(p)
fmt.Println(p.name)
fmt.Println(p.age)


With this, we end this blog on some advanced data types in GO.
If you missed the basic data types go through my previous blog on basic data types.
Next, I will be writing about receivers and concepts on encapsulation in Go.

Comments

Popular posts from this blog

Spidering the web with Python

Word Vectorization

Machine Learning -Solution or Problem