Found 33676 Articles for Programming

How to measure the execution time in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 07:07:32

5K+ Views

In Go, we have a package named time that is present in Go's standard library. This time package contains different useful functions that we can use to calculate the time taken by a code block or even a function in Go.The most widely used functions are time.Sleep(), time.Since() and time.Now(). In this article, we will see how to use all these functions.Let's first consider a very basic example where we will use all these functions.Example 1Consider the code shown below.package main import (    "fmt"    "time" ) func main() {    fmt.Println("Measuring time in Go")    start := time.Now() ... Read More

How to use closures in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 07:01:45

473 Views

In order to understand what closures are, we must know what anonymous functions are and how we can use them.Anonymous functionsIn Go, anonymous functions are those functions that don't have any name. Simply put, anonymous functions don't use any variables as a name when they are declared.We know that we declare a function with a similar syntax as shown below.func Sample(){    // some code }While we do have a name for the above function (Sample), in the case of anonymous functions, we don't have one.What is the closure function?Closure functions are the anonymous functions that have access to their ... Read More

How to use functions from another package in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 06:46:20

4K+ Views

We know that every code in Golang is present inside a package, which can either be an executable one or a utility one. The executable package name is usually main and the name of the utility package can be anything, in most cases, it is the name of the folder.Suppose we have a directory structure that looks something like this.. |-- greet |    `-- greet.go |-- sample |    `-- sample.goWe have two directories, namely, greet and sample, and each of them contains a single .go file inside them. Now, we want to make use of a function that ... Read More

How to read a file into a string in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 06:24:17

9K+ Views

To read a file into a string, we need to make use of the io/ioutil package that Go's standard library provides us with.Inside the io/ioutil package, there's a function called ReadFile() which is used to open a file and then convert its contents into a slice of bytes, and if for some reason, it isn't able to do so, then it will return an error too.Here is the syntax of the ReadLine() function.func ReadFile(filename string) ([]byte, error)It should be noted that if the above function has made a successful call, then it will return err == nil, not err == ... Read More

How to parse JSON files in Golang?

Mukul Latiyan
Updated on 21-Feb-2022 06:19:53

10K+ Views

Suppose we want to read a JSON file in Go. By reading, we mean that we want to convert the file data into a struct in Go.Consider the JSON file shown below that we will use to read and then convert the data into the structure.{    "users": [       {          "name": "Mukul Latiyan",          "social": {             "facebook": "https://facebook.com/immukul",             "twitter": "https://twitter.com/immukul",             "linkedin": "https://linkedin.com/immukul"          }       },   ... Read More

Java program to get input from the user

Manisha Chand
Updated on 18-Jun-2025 17:03:39

2K+ Views

In this article, we will understand how to get input from the user in Java. Java provides a built-in Scanner Class that belongs to the java.util package. The Scanner class is used to get user input. In this article, we will learn how to take different types of input, like integers, floating numbers, strings, etc., from the user in Java. Algorithm Following are the steps to get input from the user - Step 1- START Step-2- Import the Scanner class Step 3- Create a Scanner object Step 4- Prompt the user to enter ... Read More

Java program to read the number from standard input

Manisha Chand
Updated on 18-Jun-2025 18:17:59

2K+ Views

In Java, to read input from standard input(via keyboard), Java provides a built-in Scanner Class. In this article, we will understand how to read a number from standard input(via keyboard) in Java. The Scanner.nextInt() method of the Scanner class belongs to java.util package is used to read the number. The java.util.Scanner.nextInt() method scans the next token of the input as an int. The nextInt() method reads numbers in base-10 (decimal numbers) by default. To read numbers in other bases, like binary or hexadecimal, you can use nextInt(radix). Problem Statement Write a Java program to read a number from standard input. ... Read More

Compute the bit-wise AND of a One Dimensional and a zero-dimensional array element-wise in Numpy

AmitDiwan
Updated on 18-Feb-2022 11:54:16

109 Views

To compute the bit-wise AND of a 1D and a 0D array element-wise, use the numpy.bitwise_and() method in Python Numpy. Computes the bit-wise AND of the underlying binary representation of the integers in the input arrays. This ufunc implements the C/Python operator &.The 1st and 2nd parameter are the arrays, only integer and boolean types are handled. If x1.shape != x2.shape, they must be broadcastable to a common shape. The where parameter is the condition broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array ... Read More

Return the data of a masked array as an ndarray

AmitDiwan
Updated on 18-Feb-2022 11:51:50

226 Views

To return the data of a masked array as an ndarray, use the ma.getdata() method in Python Numpy. Returns the data of a (if any) as an ndarray if a is a MaskedArray, else return a as a ndarray or subclass if not.The subok parameter suggest whether to force the output to be a pure ndarray (False) or to return a subclass of ndarray if appropriate (True, default).A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans ... Read More

Return the mask of a masked array when mask is equal to nomask

AmitDiwan
Updated on 18-Feb-2022 11:48:53

127 Views

To return the mask of a masked array, use the ma.getmaskarray() method in Python Numpy. Returns the mask of arr as an ndarray if arr is a MaskedArray and the mask is not nomask, else return a full boolean array of False of the same shape as arr.A masked array is the combination of a standard numpy.ndarray and a mask. A mask is either nomask, indicating that no value of the associated array is invalid, or an array of booleans that determines for each element of the associated array whether the value is valid or not.StepsAt first, import the required ... Read More

Advertisements