Articles on Trending Technologies

Technical articles with clear explanations and examples

What are WaitGroups in Golang?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 258 Views

There may be instances in Golang where the execution of different goroutines may cause unexpected behaviour. In such cases, we want to make sure that certain goroutines work in a predefined manner and the program waits for all goroutines that are launched from the main function to wait. To do that, we use of WaitGroups.WaitGroups allow us to tackle the problem mentioned above as they block the code until any goroutines within the WaitGroup has been successfully executed.A WaitGroup has three exported methods that we make use of. These are −Add(int) – Increases the counter.Wait() – Blocks the execution until ...

Read More

How to use Mutex in Golang?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 478 Views

In order to understand why mutex in Go play a significant role to write better and accurate concurrent programs, we must first be aware of the concept called Race Conditions. Let's first understand what Race conditions are and how we can write a concurrent program that features a race condition and how we can then introduce mutex in that program to make it accurate.Race ConditionA race condition is a condition in which multiple goroutines are trying to access and modify the same resource. It could be the case that one goroutines is trying to increase a value of a particular ...

Read More

How to use Tickers in Golang?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 8K+ Views

There are often cases where we would want to perform a particular task after a specific interval of time repeatedly. In Golang, we achieve this with the help of tickers.We can use them with goroutines as well so that we can run these tasks in the background of our application without breaking the flow of the application.The function that we use in tickers is the NewTicker() function which takes time as an argument and we can supply seconds and even milliseconds in it.Example 1The following example demonstrates how we can use a ticker in Golang. Consider the code shown below.package ...

Read More

How to handle signals in Golang?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 2K+ Views

Before discussing signals and how to handle them, let's talk about a common scenario of creating a signal. A signal can be passed from the terminal, by either terminating the program with the help of CTRL+C or we can by default call the Exit() function that the os package provides us with.Example 1Let's consider an example where we will call the os.Exit() function just after a function declaration that has been deferred.Consider the code shown below.package main import (    "fmt"    "os"    "time" ) func main() {    defer func() {       fmt.Println("Inside the ...

Read More

How to use Timeouts in Golang

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 3K+ Views

Timeouts play an important role when we don't want to wait for the output for some goroutines that are taking more time than what they should take. It should be noted that Go directly doesn't support timeouts, but we can implement them without any difficulty.Let's suppose we have a case where we want to receive some value from a channel ch, but we don't want to wait for more than 3 seconds for the value to arrive. If we get the output after the required 3 seconds, then we want to discard it and print a different message instead of waiting for ...

Read More

Find the column number with largest value for each row in an R matrix.

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 01-Nov-2021 542 Views

To check which column has the largest value for each row in an R matrix, we can use apply function.For Example, if we have a matrix called M then we can find column that has the largest value for each row by using the command given below −apply(M,1,which.max)Example 1Consider the matrix given below −M1

Read More

Empty Slice vs. Nil Slice in Golang

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 1K+ Views

In this article, we will see the differences and similarities that are there between a slice that is declared as an empty and a nil slice.Slices in Golang are used to store a sequence of elements. Slices can be expanded at any time and they are declared in the same way as an array, but without their capacity defined.Nil SliceA nil slice is a slice in which while declaring, we simply define the name and the data type and not make use of the built-in make function.A nil slice is a slice that has nil as its zero value and ...

Read More

Functions vs Methods in Golang

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 6K+ Views

In this article, we will learn what similarities and differences are there between functions and methods in Golang. We will start with each of them separately, and then we will see an example where both of them will be used.Let's start with functions and see what they are and how to use them in Golang.Functions in GolangFunctions in Golang are a block of code that take a few parameters as input and produce some output. If we pass in the same input to a function, it will produce the same output as always.Example 1In this example, we are creating a ...

Read More

Find the column index of least value for each row of an R matrix

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 01-Nov-2021 601 Views

To find the column index of least value for each row in an R matrix, we can use apply function.For Example, if we have a matrix called M then we can find column that has the least value for each row by using the command as follows −apply(M,1,which.min)Example 1Consider the matrix given below −M1

Read More

Channel synchronization in Golang

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 1K+ Views

We can make use of channels if we want to synchronize goroutines. By synchronizing, we want to make the goroutines work in a defined manner, for example, not starting the next goroutine until the previous one has finished its execution.The channels help in achieving that, as they can be used to block the process and then can also be used to notify the second goroutine that the previous goroutine has finished its job.Example 1Let's consider a very basic example of channel synchronization where we will see how we can achieve it with the help of a buffered channel.Consider the code ...

Read More
Showing 37721–37730 of 61,248 articles
Advertisements