Programming Articles - Page 925 of 3363

How to use Tickers in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:28:20

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 create bar plot with gradient colors using ggplot2 in R?

Nizamuddin Siddiqui
Updated on 12-Nov-2021 04:01:29

6K+ Views

To create bar plot with gradient colors using ggplot2, we can use scale_fill_gradient function where we can set the lower and higher color values.For Example, if we have a data frame called df that contains two columns say Cat and Count then we can create the bar plot with gradient colors by using the below command −ggplot(df,aes(Cat,Count,fill=Cat))+geom_bar(stat="identity")+scale_fill_gradient(low="blue",high="red")ExampleFollowing snippet creates a sample data frame −x

How to use Reflection in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:24:26

657 Views

Reflection in Golang is about getting to know the data types of the data that we are dealing with at runtime. There are often certain scenarios where we would want to know the data type of a certain variable that we are getting at runtime or something similar.With the help of reflection, we can extract the type, values and kind of any data type in Golang.In this article, we will explore different third-party functions that are used in reflection.Example 1 – reflect.TypeOf()It is used to return the value of type reflect. In simple words, it is used to know what ... Read More

How to handle signals in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:21:04

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

Create ggplot2 graph with darker axes labels, lines and titles in R

Nizamuddin Siddiqui
Updated on 12-Nov-2021 04:00:20

781 Views

To create a ggplot2 graph with darker axes labels, darker lines, and dark titles, we can use theme_classic function of ggplot2 package with base_size argument set to a larger value.For Example, if we have a data frame called df that contains two columns say x and y then we can create the scatterplot between x and y using ggplot2 with darker axes labels, darker lines, and dark titles by using the below command −ggplot(df,aes(x,y))+geom_point()+theme_classic(base_size=22)ExampleFollowing snippet creates a sample data frame −x

How to use Timeouts in Golang

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:16:42

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

Empty Slice vs. Nil Slice in Golang

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:10:52

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
Updated on 01-Nov-2021 07:08:11

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 number with largest value for each row in an R matrix.

Nizamuddin Siddiqui
Updated on 01-Nov-2021 07:11:25

526 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

Decorator function pattern in Golang

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 08:00:50

985 Views

Decorator function pattern is a pattern that is mainly found in Python and JavaScript, but we can also use it in Golang.It is a pattern in which we can add our own functionality over a current function by wrapping it. Since functions in Golang are considered firstclass objects, which in turn means that we can pass them as arguments as we would in the case of a variable.Example 1Let's start with a very simple example to understand the basic case of passing a function as an argument to an already existing function.Consider the code shown below.package main import ( ... Read More

Advertisements