Remove All Items from Hash Collection in Golang

Akhil Sharma
Updated on 03-Apr-2023 13:52:55

354 Views

In Golang we can use delete keyword or empy map to remove all the elements from hash collection. Hashmap is a data structure from hash collection. It is an implementation of hash table which takes O(1) constant time to execute. In this program we will remove all items from hash collection using two methods. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments. func range(variable) The range function is used to iterate over ... Read More

Get Size of Hash Collection in Go

Akhil Sharma
Updated on 03-Apr-2023 13:52:10

395 Views

In Golang we can use various internal functions or packages like len() function or reflect package, to get the size of the hash collection. A Hash collection contains a hashmap which is generally used to create key:value pairs and on the basis of that it shortens the execution time. In this program, we have used two examples to get the size of hash collection. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments. func ... Read More

Search Item in Hash Collection using Go

Akhil Sharma
Updated on 03-Apr-2023 13:31:07

277 Views

In this article, we will write Golang programs to search an item in the hash collection. A hashmap is part of hash collections. Here we are building a hashmap as set of key-alue pairs using Golang’s map which is constructed using map literal. We will execute this program using two examples. Syntax func range(variable) The range function is used to iterate over any data type. To use this we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element ... Read More

Iterate Hash Collection and Print Only Values in Go

Akhil Sharma
Updated on 03-Apr-2023 13:23:09

364 Views

In golang we can use hashmap and key functions to iterate hash collection. Hashmap is said to be one of the hash collections. In the hashmap the data is stored in key value pairs as we did below. In this program we will use three methods to iterate the hashmap and print its values. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments func append(slice, element_1, element_2…, element_N) []T The append function is ... Read More

Get Current Weekday in Go

Akhil Sharma
Updated on 03-Apr-2023 13:22:12

483 Views

In this Go language article, we will write proCurrent weekday implies the current day in the week which can be obtained using numerous methods. In this program we will use three examples to get the current weekday for which time package will be imported in the program, it helps in viewing the time. Syntax func Now() Time The Now() function is defined in time package. this function generates the current local time. To use this function, we have to first import the time package in our program. time.Format() This function belongs to the time package. It is used ... Read More

Get Current Time Zone in Go

Akhil Sharma
Updated on 03-Apr-2023 13:19:11

6K+ Views

Golang has internal functions like Now() Time, and other Time functions that can be used for getting the current time zone. Current time zone in Go is a time zone where people reside in and see the same standard time. In a country like India IST is observed throughout the nation. In this program we will discuss how to obtain current time zone using two examples. Syntax func Now() Time The Now() function is defined in time package. this function generates the current local time. To use this function, we have to first import the time package in ... Read More

Create Hash Collection in Go

Akhil Sharma
Updated on 03-Apr-2023 13:18:29

233 Views

In this article, we will write a Golang programs to create hash collections with the help of make function and map function. Hashmap stores key value pairs that can be accessed via index. In this program we will create a hash collection with the help of two examples. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments. Using Make Function In this method, we will create a hashmap with the help of a make ... Read More

Golang Program to Demonstrate a Simple Module

Akhil Sharma
Updated on 03-Apr-2023 13:17:34

206 Views

In this Go language article, we will write programs to demonstrate a simple module. A module in Go is called as the collection of packages namely we can call it packages and sub-packages where the root file will be called as go.mod. In this program we will demonstrate a module using three different examples. Syntax func make ([] type, size, capacity) The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments Example1: Golang program to demonstrate a simple module using basic printing ... Read More

Binary Search on Slice of Custom Structs in Golang

Akhil Sharma
Updated on 03-Apr-2023 13:14:13

958 Views

In this Golang article, we are going to perform binary search on a slice of custom structs using iterative and recursive method. Binary search is a search algorithm used to find the position of a specific value within a sorted sequence of elements. Algorithm Step 1 − First, we need to import the fmt package. Step 2 − Create a custom Person structure with string type name and integer type age. Step 3 − Now, create a bSearch() function that is used to perform binary search on a slice of Person struct. Step 4 − It initializes the low ... Read More

Check if a Stack is Empty in Go

Akhil Sharma
Updated on 03-Apr-2023 13:13:45

286 Views

In this Golang article, we are going to check if a stack is empty using iterative and recursive method. A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle. Algorithm Step 1 − First, we need to import the fmt and strconv package. Step 2 − Create a stack structure with an item slice to store the stack elements. Step 3 − Now, define the functions, push(), pop() and peek() to perform the basic operations with the stack. Step 4 − push() adds the item to the end of the stack while pop() removes the last ... Read More

Advertisements