Found 1062 Articles for Go Programming

Golang program to convert primitive types to objects

Akhil Sharma
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

Akhil Sharma
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

Akhil Sharma
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

Akhil Sharma
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

Golang program to find the duplicate characters in the string

Akhil Sharma
Updated on 20-Feb-2023 10:50:32


In golang, identifying characters that appear more than once in a string is the first step in finding duplicate characters. To accomplish this, iterate through the string while keeping track of the characters that have already been encountered. Then, before adding a character, determine if it is already in the set of characters that have already been encountered. A character is a duplication if it already exists in the set. Let’s see its execution. Method 1: Using map and a for loop In this method, the program uses a map to keep track of the number of times each character ... Read More

Golang program to reverse a string using stacks

Akhil Sharma
Updated on 20-Feb-2023 10:49:36


In this article, we will see how to reverse a string using stacks with Go Programming. A stack is a Last In First Out (LIFO) compliant data structure in Go. This implies that the first element to be eliminated will be the one that was most recently added to the stack. A doubly-linked list is implemented in the built-in Go package "container/list" and can be used to build a stack. Slices (dynamic arrays) are frequently used to implement stacks in Go, though. Let’s see how it’s executed using different set of examples. Method 1: Converting the string to slice of ... Read More

Golang program to show use of super keyword in class

Akhil Sharma
Updated on 20-Feb-2023 10:43:58


In golang, the "super" term doesn't exist in Go. The "super" keyword is used in object-oriented programming languages to denote the parent class or the class that is being inherited from. It is often used to access parent class methods or properties that the child class has modified. In this article, we will see how to use super keyword in the class with different examples. Method 1: Using child struct and parent struct In this Method, we will learn how to use super keyword in class using child struct and parent struct. Here, a Parent struct is embedded in the ... Read More

Golang program to remove all whitespaces from a string

Akhil Sharma
Updated on 20-Feb-2023 10:47:10

2K+ Views

When we remove whitespaces from a string it means removing blank spaces, tabs, and other white space characters, including newline characters. This can be helpful for parsing user input or working with data in a particular format, among other instances where we wish to clean up or standardize a string before processing it further. There are various ways to eliminate whitespace from a string. A few examples include: Using the TrimSpace function, whitespace characters at the beginning and end of a string are eliminated. Using the Replace function to replace any whitespace in the string with a string that is ... Read More

Golang program to compare two strings lexicographically

Akhil Sharma
Updated on 20-Feb-2023 10:41:54


In Golang, lexicographic comparison refers to comparing strings in a manner similar to how they would be compared in a dictionary where the words are listed alphabetically. This means that the initial character of each string is used to compare the individual characters of two strings in order. The comparison process continues until a difference is discovered or one of the strings' ends is reached if the initial few characters are the same. The string that appears first in the lexical order is regarded as being "less than" the other. Let’s see via examples how to execute this program. Method ... Read More

Golang program to print first letter of each word using regex

Akhil Sharma
Updated on 20-Feb-2023 10:41:04


A string of characters known as a regular expression (regex or regexp) creates a search pattern. Regular expressions are frequently employed to carry out string matching, sometimes known as "find and replace, " or pattern matching with strings. Input validation, parsing, and other tasks are also possible with them. Regular expressions use special characters and metacharacters to specify the pattern to be matched, and their syntax differs widely between computer languages. Let’s see different Golang examples to get a clear view of the concept. Method 1: Using regex.MustCompile() function In this example, we will see how to print first ... Read More