Measure 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

Use Closures in Golang

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

481 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

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

Read a File into a String in Go

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

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

Compute Bit-wise AND of One-Dimensional and Zero-Dimensional Arrays in NumPy

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

118 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 Data of a Masked Array as ndarray

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

229 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 Mask of a Masked Array When Mask is Equal to NoMask

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

137 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

Compute Bitwise AND of 1D and 2D Arrays Element-wise in NumPy

AmitDiwan
Updated on 18-Feb-2022 11:44:28

237 Views

To compute the bit-wise AND of a 1D and a 2D 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 will retain ... Read More

Compute Bit-wise AND of Two 1D NumPy Arrays Element-wise

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

166 Views

To compute the bit-wise AND of two 1D arrays 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 will retain its original ... Read More

Advertisements