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

Updated on 20-Feb-2023 16:14:56
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

Updated on 20-Feb-2023 16:13:46
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

Updated on 20-Feb-2023 16:12:44
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

Updated on 20-Feb-2023 15:33:10
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

Updated on 20-Feb-2023 15:31:01
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

Updated on 20-Feb-2023 15:29:15
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

## Golang program to convert primitive types to objects

Updated on 20-Feb-2023 15:23:48
In this Golang program, converting primitive data types (such as int, char, float, etc) to objects is known as "Boxing". It is a procedure that transforms a primitive type into the matching object type, such as an integer into a float, a character into a char, etc. This enables the programmer to use the fields and methods of the relevant object type and treat primitive data types as objects. We will use two methods to execute this program. Method 1: Using Reflect In this method, the primitive types are transformed into objects using the reflect package. The type and data ... Read More

## Golang program to call one constructor from another

Updated on 20-Feb-2023 15:22:35
In Go programming language, a constructor is a specific kind of function used to initialize an object's state when it is initially formed. It is used to initialize variables and provide data for the objects. We will execute this program using two methods and we will use struct in both of these examples with anonymous struct in the second example. Let’s see through these examples to understand how the program is executed. Method 1: Using a function that returns a desired type In this method, to construct a new Child struct, the NewChildWithAge function runs the NewChildfunction, sets the age, ... Read More

## Golang Program to Check Whether the Given String is Pangram

Updated on 20-Feb-2023 10:54:12
In this article, we will understand different golang examples to check whether a given string is pangram. A statement known as a pangram uses each letter of the alphabet at least once. A pangram is a string in the programming language Golang that contains every letter of the alphabet, regardless of case. Syntax strings.ToLower(str) Using strings in Go (golang), you can change a string's case to lowercase. The strings package's ToLower() function. 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 ... Read More

## Golang program to implement switch statement on a strings

Updated on 20-Feb-2023 10:52:47
A switch statement in Golang is a control flow statement that enables you to run one block of Example: in response to various situations. Similar to if-else expressions, it can test several conditions more quickly. In this article, we will see how switch statement is implemented on strings in Go. Method 1: Using default case as fallback option In this method, if a match is detected, this program will verify the input variable, compare it to the cases, and execute the associated block of Example:; otherwise, it will run the default case block. Let’s see through the Example: and algorithm ... Read More
Previous 1 ... 4 5 6 7 8 ... 55 Next