Found 26504 Articles for Server Side Programming

How to check if a string is a valid URL in Golang?

Kiran Kumar Panigrahi
Updated on 01-Nov-2021 07:41:23

5K+ Views

There are cases where we would want to know if the URL that we got from an http request in the form of a string is even valid or not. In such cases, one can use two functions that the net/url package of Go's standard library provides.Example 1The first basic case is to check if the URL is well structured and a valid one, and for that, we can use the ParseRequestURI() function of the URL package.Consider the code shown below.package main import (    "fmt"    "net/url" ) func main() {    u, err := url.ParseRequestURI("http://golangcode.com")   ... Read More

How to split a string in Golang?

Kiran Kumar Panigrahi
Updated on 03-Mar-2022 13:33:43

4K+ Views

Splitting a string means being able to get a part of a string, and that part can either be based on a position or a character in the string.In Go, if we want to split a string on the basis of positions, we can make use of the [ ] (square brackets) and then pass the indices in them separated by a colon.SyntaxConsider the syntax shown below.sl[startingIndex : EndingIndex]It should be noted that the element at the EndingIndex in the slice won't be considered, as the range is from startingIndex to (EndingIndex−1).Example 1Now, let's consider an example where we will ... Read More

What are WaitGroups in Golang?

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

217 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
Updated on 01-Nov-2021 07:33:12

434 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 replace 0 with NA in an R matrix?

Nizamuddin Siddiqui
Updated on 01-Nov-2021 07:39:43

3K+ Views

To replace 0 with NA in an R matrix, we can use subsetting with single square brackets and then set zeros to NA.For Example, if we have a matrix called M that contains some zeros then we can replace 0 with NA by using the command mentioned below −M[M==0]

How to use Tickers in Golang?

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

7K+ 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

611 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

1K+ 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

744 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

Advertisements