0% found this document useful (0 votes)
19 views14 pages

GO Mod 3

The document provides an overview of arrays and maps in the Go programming language, detailing how to declare, access, and manipulate arrays and slices. It explains the differences between arrays and slices, the process of passing slices to functions, and the creation and initialization of maps. Additionally, it covers sorting maps by keys and values, as well as inverting a map's elements.

Uploaded by

bagheera.gunnu18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views14 pages

GO Mod 3

The document provides an overview of arrays and maps in the Go programming language, detailing how to declare, access, and manipulate arrays and slices. It explains the differences between arrays and slices, the process of passing slices to functions, and the creation and initialization of maps. Additionally, it covers sorting maps by keys and values, as well as inverting a map's elements.

Uploaded by

bagheera.gunnu18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

MODULE-III: ARRAYS AND MAPS

Arrays Declaration/Accessing:

Arrays in Golang or Go programming language is much similar to other


programming languages. In the program, sometimes we need to store a
collection of data of the same type, like a list of student marks. Such type of
collection is stored in a program using an Array. An array is a fixed-length
sequence that is used to store homogeneous elements in the memory. Due to
their fixed length array are not much popular like Slice in Go language. In an
array, you are allowed to store zero or more than zero elements in it. The
elements of the array are indexed by using the [] index operator with their
zero-based position, which means the index of the first element is array[0] and
the index of the last element is array[len(array)-1].

Creating and accessing an Array


In Go language, arrays are created in two different ways:

Using var keyword: In Go language, an array is created using the var keyword
of a particular type with name, size, and elements.

Syntax:
Var array_name[length]Type

Important Points:
In Go language, arrays are mutable, so that you can use array[index] syntax to
the left-hand side of the assignment to set the elements of the array at the
given index.
Var array_name[index] = element

●​ You can access the elements of the array by using the index value or by

using for loop.

●​ In Go language, the array type is one-dimensional.

●​ The length of the array is fixed and unchangeable.

●​ You are allowed to store duplicate elements in an array.


Output:
Multidimensional arrays:

As we already know that arrays are 1-D but you are allowed to create a
multi-dimensional array. Multi-Dimensional arrays are the arrays of arrays of
the same type. In Go language, you can create a multi-dimensional array using
the following syntax:
Array_name[Length1][Length2]..[LengthN]Type

You can create a multidimensional array using Var keyword or using shorthand
declaration as shown in the below example.

Note: In a multi-dimension array, if a cell is not initialized with some value by


the user, then it will initialize with zero by the compiler automatically. There is
no uninitialized concept in the Golang.
Slices in Golang
Slices in Go are a flexible and efficient way to represent arrays, and they are
often used in place of arrays because of their dynamic size and added features.
A slice is a reference to a portion of an array. It’s a data structure that describes
a portion of an array by specifying the starting index and the length of the
portion. This allows you to work with a portion of an array as if it were an
independent array. In Go language slice is more powerful, flexible, convenient
than an array, and is a lightweight data structure. Slice is a variable-length
sequence that stores elements of a similar type, you are not allowed to store
different type of elements in the same slice. It is just like an array having an
index value and length, but the size of the slice is resized they are not in
fixed-size just like an array. Internally, slice and an array are connected with each
other, a slice is a reference to an underlying array. It is allowed to store duplicate
elements in the slice.

The first index position in a slice is always 0 and the last one will be (length
of slice – 1).

Here’s an example that demonstrates how to create a slice in Go:


In this example, the array is created with 5 elements, and the slice is created by
specifying the starting index 1 and the length 4. The slice now contains the
elements 2, 3, and 4 from the original array.

Components of Slice

A slice contains three components:

●​ Pointer: The pointer is used to points to the first element of the array

that is accessible through the slice. Here, it is not necessary that the

pointed element is the first element of the array.

●​ Length: The length is the total number of elements present in the

array.

●​ Capacity: The capacity represents the maximum size upto which it can

expand.
Passing a slice to a function:

How to pass a Slice to Function in Golang?


Golang, also known as Go, is a popular programming language that is known for its

simplicity, efficiency, and performance. Slices are an important data structure in

Golang that allows us to work with a sequence of elements of the same type. In this

article, we'll discuss how to pass a slice to a function in Golang, and provide a

step-by-step guide on how to achieve this task.

What is a Slice in Golang?


A slice in Golang is a dynamic array, which means it can grow or shrink as needed. It's

a powerful data structure that allows us to work with a sequence of elements of the

same type, and is commonly used in Golang programs to manage collections of data.

Why Pass a Slice to a Function?


In many cases, we need to pass a slice to a function to perform operations on the

elements of the slice. For instance, when we're building a data processing application

that requires sorting or filtering of data, or when we need to manipulate the elements

of a slice in some way.

How to Pass a Slice to a Function in Golang?


In Golang, we can pass a slice to a function by simply passing the slice as an

argument to the function. The function can then work with the elements of the slice as

needed.

Example
Here's an example of how to pass a slice to a function −

In the above example, we first declare a slice of integers numbers with the values 1, 2,

3, 4, and 5. Then we call the sum() function and pass the numbers slice as an

argument. The sum() function then calculates the sum of the elements of the slice and

returns the result. Finally, we print the result to the console using the Println() function.
Difference between new() and make():

Concept
The new keyword in Golang is used to create a new instance of a variable, and it

returns a pointer to the memory allocated.

The make keyword in Golang is used to create slices, maps, and channels, and it

returns a value of the type that was created.

Differences
1.​ The new function in Go can be used with any type, including built-in

types, user-defined types, and pointer types. The make is also a built-in

function in Go, but it is used to create slices, maps, and channels,

which are reference types that require initialization.

2.​ The new allocates zeroed memory, while the make allocates memory and

initializes it.

3.​ The zero value of the specified type is returned, which means that the newly

allocated value will be initialized to its zero value. The make initializes the

memory it allocates and returns a non-zero value of the specified

type.
Maps: Declaration, initialization

Go Maps
Maps are used to store data values in key:value pairs.

Each element in a map is a key:value pair.

A map is an unordered and changeable collection that does not allow duplicates.

The length of a map is the number of its elements. You can find it using the
len() function.

The default value of a map is nil.

Maps hold references to an underlying hash table.

Go has multiple ways for creating maps.

In Go language, maps can create and initialize using two different ways:

1. Simple: In this method, you can create and initialize a map without the use of
make() function:

Creating Map: You can simply create a map using the given syntax:
2. Using make function: You can also create a map using make() function. This
function is an inbuilt function and in this method, you just need to pass the type
of the map and it will return an initialized map.
Deleting an element in maps:

Sorting a map:

Go is a powerful programming language that comes with built-in support for maps.

Maps are unordered collections of key-value pairs, and sometimes it's necessary to

sort them by either their keys or values. Fortunately, Go provides a way to sort maps

by both keys and values using the sort package. In this article, we'll discuss how to

sort a Golang map by keys or values.

Sort Golang Map By Keys


To sort a Golang map by keys, we first need to extract the keys from the map and sort

them using the sort package. We can then iterate over the sorted keys and retrieve

their corresponding values from the map.


Sort Golang Map By Values
To sort a Golang map by values, we need to create a slice of key-value pairs and sort

it based on the values. We can then extract the sorted keys from the slice.

Inverting a map:(doubt)

There is no built in function in Golang that allow you to reverse the elements order in
a map, but it can be done easily with for loops. However, care must be taken to
ensure that the reversed elements are stable. At this point of writing, the only way to
ensure the reversed elements order is stable is to put them in a slice.

Refer here for more info

You might also like