Found 1082 Articles for Go Programming

Golang Program that switch on floating-point numbers

Siva Sai
Updated on 18-Apr-2023 10:48:11

156 Views

In Go, the switch statement can also be used with floating-point numbers. This feature can be useful in certain scenarios, where a floating-point value needs to be compared against different thresholds or ranges. In this article, we will go through an example program that demonstrates the use of switch statement on floating-point numbers. Syntax of Switch Statement with Float The syntax of switch statement with floating-point numbers is the same as that with any other type. The only difference is that the cases need to be specified as floating-point values. switch expression { case value1: ... Read More

Golang program that removes duplicates, ignores order

Siva Sai
Updated on 18-Apr-2023 12:55:53

62 Views

When working with slices in Golang, it's common to need to remove duplicate elements from the slice. While there are many ways to do this, one approach that can be particularly useful is to remove duplicates while ignoring the order of the elements. This can be useful, for example, when you want to compare two slices for equality without caring about the order of the elements. In this article, we'll explore a simple Golang program that removes duplicates from a slice while ignoring the order of the elements. We'll break down the program step by step to explain how it ... Read More

Golang Program that Removes Duplicates Using Nested Loops

Siva Sai
Updated on 18-Apr-2023 10:42:54

84 Views

Removing duplicates from a slice or an array is a common problem in programming. One of the ways to solve this problem in Golang is by using nested loops. In this article, we will write a Golang program that removes duplicates from a slice using nested loops. Understanding the Problem Before writing the program, let's understand the problem statement. Suppose we have a slice with some duplicate elements − numbers := []int{1, 2, 3, 1, 4, 2, 5} Our task is to remove the duplicate elements from this slice and get the unique elements. The final slice should look ... Read More

Golang Pointer to an Array as Function Argument

Siva Sai
Updated on 18-Apr-2023 10:42:02

2K+ Views

Golang allows us to use pointers to access the value of an object in memory. Using pointers can improve the performance of the code by reducing the memory allocation and enhancing the efficiency of the program. In this article, we will explore how to use a pointer to an array as a function argument in Golang. Pointer to an Array in Golang In Golang, an array is a fixed-length data structure that stores a collection of elements of the same type. When we pass an array to a function as an argument, a copy of the array is created, and ... Read More

Splitting a slice of bytes after the specified separator in Golang

Sabid Ansari
Updated on 18-Apr-2023 10:40:46

180 Views

In Golang, it is often required to split a slice of bytes after a specific separator. This can be done using the built-in function bytes.SplitAfter(). This function splits a slice of bytes into sub-slices after each occurrence of a specified separator. In this article, we will discuss how to split a slice of bytes after a specific separator in Golang. Syntax of bytes.SplitAfter() The syntax of the bytes.SplitAfter() function is − func SplitAfter(s, sep []byte) [][]byte where s is the slice of bytes to be split, and sep is the separator. Example package main import ( ... Read More

Searching an element of int type in slice of ints in Golang

Sabid Ansari
Updated on 18-Apr-2023 10:13:22

57 Views

Slices are an essential feature of Go programming language that allows you to store and manipulate an ordered collection of elements. Slices are similar to arrays, but unlike arrays, they are dynamic and can be resized. In Go, you can perform a search operation on a slice to find an element or a set of elements. This article will guide you through the process of searching for an element of int type in a slice of ints in Go. Searching an element of int type in slice of ints in Golang To search for an element of int type in ... Read More

Replacing all String which Matches with Regular Expression in Golang

Sabid Ansari
Updated on 18-Apr-2023 10:12:34

3K+ Views

In Golang, you can replace all the string occurrences that match a regular expression with the help of the regexp package. The regexp package in Golang provides a ReplaceAllString() function that replaces all occurrences of the matched string with a new string. Example Here's an example of how to replace all the string occurrences that match a regular expression in Golang − package main import ( "fmt" "regexp" ) func main() { str := "The quick brown fox jumps over the lazy dog" re := regexp.MustCompile("fox|dog") ... Read More

Polymorphism Using Interfaces in Golang

Sabid Ansari
Updated on 18-Apr-2023 10:11:43

2K+ Views

Polymorphism is a powerful concept in object-oriented programming that allows you to write flexible and reusable code. In Go, you can achieve polymorphism using interfaces.  An interface is a collection of method signatures that any type can implement. This means that you can write code that can work with any type that implements an interface, without knowing the underlying type. In this article, we will discuss how to achieve polymorphism using interfaces in Golang. What is Polymorphism? Polymorphism is the ability of an object to take on many forms. In object-oriented programming, polymorphism is achieved when an object can be ... Read More

How to find the index of rune in the string in Golang?

Sabid Ansari
Updated on 18-Apr-2023 10:10:57

574 Views

Golang offers many built-in functions and packages to perform string operations. One common operation is finding the index of a particular rune in the string. In this article, we will explore how to find the index of a rune in a string in Golang. Rune in Golang is an alias for int32 and represents a Unicode code point. Each character in a string is a rune. The string package in Golang provides several functions for handling runes in a string. Let's take a look at how to find the index of a rune in a string in Golang. Using the ... Read More

Goroutine vs Thread in Golang

Sabid Ansari
Updated on 18-Apr-2023 10:10:08

6K+ Views

Goroutines and threads are both used for achieving concurrency in programming languages. In Golang, goroutines are the primary mechanism for achieving concurrency, while threads are a lower-level construct that is managed by the operating system. In this article, we will explore the differences between goroutines and threads in Golang, their benefits and limitations, and when to use each of them. Goroutines A goroutine is a lightweight thread managed by the Go runtime. Goroutines allow multiple tasks to be executed concurrently, making it easier to write concurrent programs. Goroutines are more efficient than threads because they use less memory and can ... Read More

Advertisements