Programming Articles

Page 1823 of 2547

Golang Program to Create Slice Using Composite Literal

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 261 Views

In Go programming language, a composite literal is used to create a slice, array, struct etc object of type given and initializes it. In this article, we will create a slice using three examples. In the first example, slices of numbers will be created, in the second example, slices of strings will be created and in the third example, built-in functions are used to create slices. In this way the creation of slices is demonstrated. Let’s see the examples to get a crystal-clear view of the concept. Syntax func make ([] type, size, capacity) The make function in go ...

Read More

Golang Program to Show Use of this Keyword in Class

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 439 Views

In Go programming language, there is no concept of classes so struct are used to demonstrate the use of this keyword in class. The "this" keyword refers to the current method or object that is currently being executed in the program. In this article we will use two examples to see how the program works. In the first example, we will use Child struct by calling the method on this struct and printing its name and age whereas in the second example, we will use a Rectangle struct to print its area by calling the method on this struct. Let’s ...

Read More

Golang Program to Implement a Priority Queue Using a Balanced Binary Search Tree (e.g. AVL Tree)

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 360 Views

In this article, we implement a priority queue using a balanced binary search tree, specifically an AVL tree. Here we are going to use seven  different methods: PriorityQueue struct, Node struct, insert, remove, Isempty, size as well as peek along with examples to elaborate the concept. Syntax func (pq *PriorityQueue) Insert(value interface{}, priority int) The Syntax func (pq *PriorityQueue) Insert(value interface{}, priority int) is a method declaration in Golang. It defines a method named Insert that operates on a PriorityQueue instance (receiver) represented by pq. func (pq *PriorityQueue) Remove() interface{} The Syntax func (pq *PriorityQueue) Remove() interface{} is ...

Read More

Golang Program to Implement a Priority Queue Using a Linked List

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 357 Views

A priority queue is a data structure where each element is assigned a priority and elements with higher priority are dequeued first. In this article, The Golang program focuses to implement a priority queue using a linked list. Here we are going to use seven different methods: PriorityQueue struct, Node struct, insert, remove, Isempty, size as well as peek along with examples to elaborate the concept. Syntax type PriorityQueue struct { head *Node} The Syntax type PriorityQueue struct { ... } defines a struct type called PriorityQueue in Golang. It has one field: head of type *Node. The PriorityQueue struct ...

Read More

Golang Program to Count The Possible Decodings of a Given Digit Sequence

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 193 Views

A digit sequence in go language is a set of digits that is used to represent a number. We can represent the digit sequence by using go languages existing data type. In this article, the Golang program is  designed to calculate the possible decodings of a given digit sequence. It solves this problem by using dynamic programming techniques. Given a sequence of numbers, the program calculates the number of ways numbers can be determined.Here we are going to use the method countDecodings along with examples to elaborate the concept. Syntax func countDecodings(digits string) int The countDecodings function is expected ...

Read More

Recover in Golang

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 314 Views

In Golang, the recovery mechanism provides a way to deal with panic and recover from it. Panic is an unexpected error that can cause the program to terminate. In this article, we are going to discuss what is recovered in golang. Here we are going to use two different methods: using handlePanic as well as the main() function along with examples to elaborate the concept. Syntax recover() In this Syntax, The handlePanic function is used in Go to recover from panics. It checks if a panic occurred using the recover() function, which returns the value passed to panic(). If ...

Read More

Race Condition in Golang

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 2K+ Views

In Go, race conditions occur when goroutines simultaneously read and write to the same shared memory space without synchronization mechanisms. This can cause data corruption, inconsistent states, or crashes.In this article, we are going to discuss race conditions in golang. Here we are going to use two different methods: Synchronization with WaitGroup as well as Synchronization with Mutex along with examples to elaborate the concept. Syntax sync.mutex() it is used to create a new mutex variable. The sync.Mutex type provides a way to control access to shared resources by acquiring and releasing locks mutex.Lock() This method is ...

Read More

Overview of Benchmark Testing in Golang

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 379 Views

In this article we are going to discuss an overview of Benchmark testing. Benchmark testing is a basic portion of program optimization to grade the execution and productivity of code. In Golang, benchmark testing is backed through the built-in testing bundle. Here we are going to use the Benchmark function along with examples to elaborate on the concept. Syntax time.duration() This function is used to represent the time interval in go language. It allows the user to work with time durations. time.sleep() We use this function to stop the execution of a go language program for a specific ...

Read More

Golang Program to Create an Interface Named Cache that Defines a Set and Get Method

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 273 Views

The purpose of this interface is to provide a contract for implementing cache functionality in various data structures or systems. The Set method is responsible for storing a value in the cache with a specified key, while the Get method retrieves the value associated with a given key from the cache. In this article, we will create an interface called Cache that defines two methods: Set and Get. Here we are going to use two different methods − Get(key string) interface{} as well as Set(key string, value interface{}) along with examples to elaborate the concept. Syntax Set(key string, value interface{}) ...

Read More

Golang Program to Delete a Node From a Red Black Tree

Akhil Sharma
Akhil Sharma
Updated on 20-Jul-2023 295 Views

Red Black Tree is a self-balancing binary search tree with additional properties that ensure a balanced tree structure and efficient operations. The delete operation in a Red Black Tree involves rearranging the tree and maintaining the Red Black Tree properties after removing a node. Here we are going to use three different methods:deleteNode method, delete method, and successor transplant method along with examples to elaborate the concept. In this article, The Golang program implements the deletion operation in a Red Black Tree data structure using Golang. Syntax func (t *RedBlackTree) Delete(key int) The Syntax "func (t *RedBlackTree) Delete(key int)" ...

Read More
Showing 18221–18230 of 25,466 articles
Advertisements