Found 1082 Articles for Go Programming

Golang program to add elements to a linkedlist

Akhil Sharma
Updated on 21-Feb-2023 12:09:50

1K+ Views

In Golang, we can use node struct and linkedlist struct to add elements to a linkedlist. A linkedlist is a data structure made up of a series of nodes, each of which includes an element and a reference to the node after it in the sequence. It is a linear data structure with pointers connecting the items, and from the first node (head) to the last node, each node can be visited (tail). As opposed to arrays, which require all elements to be shifted, linked lists just require altering the pointers of the adjacent nodes, making them helpful in situations ... Read More

Golang program to implement graph data structure

Akhil Sharma
Updated on 21-Feb-2023 12:05:39

1K+ Views

In Go programming language, a graph is a type of data structure made up of a limited number of nodes (also known as vertices) and a set of connecting edges. Relationships between several entities can be depicted on a graph. It can be represented by employing different data structures, such as an adjacency matrix or an adjacency list. The particular use case and the needs of the application will determine the data structure to be used. A graph can also be implemented in Go by using a library or package like go-graph. We will use two methods here to implement ... Read More

Golang program to access elements from a linked list

Akhil Sharma
Updated on 20-Feb-2023 16:17:00

214 Views

In Go Programming language, a linked list is a data structure which contains a node that further contains two values, the data and the next, where next points to the next node in the list. We will use two methods in this program to access elements from a linked list. In the first example iteration will be used and in the second example a variable current will be used to access the elements. Method 1: Using Iteration This program builds a linked list with three members and iterates through it to access and output each element's value. The output ... Read More

Golang program to remove elements from the linked list

Akhil Sharma
Updated on 20-Feb-2023 16:16:13

488 Views

In Go, a linked list is a linear data structure with pointers connecting the items, and from the first node (head) to the last node, each node can be visited (tail). We will execute the program of removing elements from the linked list using two examples. The first example uses node struct whereas the second example uses a dummy node Method 1: Using Node Struct This code creates a Node struct that has two fields: Value and Next, which links to the next node in the list. The remove_node method removes the node with the supplied value from the list ... Read More

Golang program to add elements at first and last position of linked list

Akhil Sharma
Updated on 20-Feb-2023 16:14:56

174 Views

In golang, a linked list is a unique data structure in which there is a value in the node and the next pointer which points to the next node. The list's initial node is referred to as the head, while the list's last node which points to nil depicts the end of list. We will add elements at the first and last position of the linked list using two examples. In the first example node struct will be used and in the second example ListNode struct will be used. Method 1: Using Node Struct In this method, we will use ... Read More

Golang program to implement the Queue data structure

Akhil Sharma
Updated on 20-Feb-2023 16:13:46

2K+ Views

In this Golang program, a queue is a data structure that works on the First-In-First-Out (FIFO) principle where elements are added to the rear and removed from the front. Although Go doesn't come with a built-in queue data structure, slices, linked lists, and other data structures can be used to build one. We will use two methods to implement queue data structure using slices and linked list. Method 1: Using Slice Method The fundamental operations for a queue data structure: Enqueue, Dequeue, and IsEmpty are implemented in this implementation, which employs a slice to hold the items in the ... Read More

Golang program to implement stack data structure

Akhil Sharma
Updated on 20-Feb-2023 16:12:44

3K+ Views

In Golang, a stack is a linear data structure that works on the Last-In-First-Out (LIFO) principle which means that the element which is pushed in the stack in last will be popped out first. We will use two methods to implement the stack data structure using slice of integers and struct. Let’s see different examples to understand the concept. Method 1: Using Slice of Integers Here, in this method, Go employs two functions, Push and Pop, to add and remove values from the top of the stack, respectively, and a slice of integers to store the values in the stack. ... Read More

Golang program to demonstrate the string interpolation

Akhil Sharma
Updated on 20-Feb-2023 15:33:10

2K+ Views

In Go programming language, string interpolation is the process of integrating expressions into a string literal. When it's necessary to include dynamic values in strings for log statements, error messages, and other purposes, string interpolation is frequently used. We will use two methods in this program to demonstrate string interpolation. The first example demonstrates use of sprintf and the second example shows how to use printf to execute string interpolation. Method 1: Using sprintf with fmt package In this method, the name and age variables' values are combined into a string using fmt.Sprintf() function which demonstrate interpolation.  Here,  Strings are ... Read More

Golang program to create a string object

Akhil Sharma
Updated on 20-Feb-2023 15:31:01

377 Views

In Golang, a string cannot be modified after they have been created. A built-in type in Go, the string type can be used in a variety of ways much like any other data type. In this article, we will use three methods to create a string object. The first method involves using of double quotes and backticks, in the second method strings.Replace() function is used and in the third method, a byte function is used to execute the program. Method 1: Using double quotes and backticks Two string objects, str1 and str2, are created in this program. Backticks (') are ... Read More

Golang program to convert vector to a list

Akhil Sharma
Updated on 20-Feb-2023 15:29:15

640 Views

In Golang, a vector is referred to as an array or a slice. Slice is a dynamic array whose size can be changed whereas a linked list data structure is one type of list, where each node has a value and is pointing to the next element. We will use two examples in this program to convert vector to a list. In the very first example a built-in copy function will be used and in the second example we will iterate the slice in the reverse order to obtain the output. Method 1: Using Copy Function In this method, we ... Read More

Advertisements