Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 926 of 3366
5K+ 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
505 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
968 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
675 Views
Golang provides us with different approaches that we can use to print custom errors. We will explore two such approaches in this article.The first approach requires us to make use of the error.New() function which will create a new error, and we can even pass a string of our choice inside it as an argument.Example 1Consider the code shown below.package main import ( "errors" "fmt" "math" ) func areaOfCircle(radius float64) (float64, error) { if radius < 0 { return 0, errors.New("Area calculation wrong, the radius is < zero") } ... Read More
555 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
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
14K+ Views
Response status codes are the numbers that we get in response that signify what type of response we received from the server when we asked for something from it.There are different status codes that one gets from the response, and these are mainly divided into five categories.Generally, the status codes are divided into these five classes.1xx (Informational)2xx (Successful)3xx (Redirection)4xx (Client Error)5xx (Server Error)In this article, we will try to get two or more of these status codes.Example 1Let's start with a basic HTTP request to the google.com URL. Once we do that, we will get the response from the server ... Read More
7K+ Views
Consider a case where we want to get the content type of a file in Golang for whatever reasons. In order to do that, we must first know how to open the file and read some of its bytes into a buffer slice which we will then pass to a function that will help us in detecting the type of the file.The first step is to open the file whose type we want to check.Open the FileConsider that we have a file named sample.pdf whose contentType is what we want to know. In order to open the file, we need ... Read More
1K+ Views
To convert days in a week to number in R data frame column, we would need to convert the column into a factor by defining the weekdays as the levels and then read that column as integer.If we provide the correct sequence of weekdays during conversion then Monday will be converted to 1. Check out the below Examples to understand how it can be done.Example 1Following snippet creates a sample data frame −Week_Days
63K+ Views
To delete a key from a map, we can use Go's built-in delete() function. It should be noted that when we delete a key from a map, its value will also be deleted as the key-value pair is like a single entity when it comes to maps in Go.SyntaxThe syntax of the delete function is shown below.delete(map, key)Once we call the function in the above format, then the key from the map will be deleted.Now, let's use the above function in Go code and understand how it works.Example 1Consider the code shown belowpackage main import ( "fmt" ) ... Read More