Mukul Latiyan

Mukul Latiyan

363 Articles Published

Articles by Mukul Latiyan

Page 31 of 37

How to measure the execution time in Golang?

Mukul Latiyan
Mukul Latiyan
Updated on 21-Feb-2022 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 functions from another package in Golang?

Mukul Latiyan
Mukul Latiyan
Updated on 21-Feb-2022 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
Mukul Latiyan
Updated on 21-Feb-2022 10K+ 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
Mukul Latiyan
Updated on 21-Feb-2022 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

Lambda Functions in Ruby

Mukul Latiyan
Mukul Latiyan
Updated on 25-Jan-2022 462 Views

In Ruby, we can take the help of lambda functions when we want to use anonymous functions. They are also treated like objects in Ruby, as everything in Ruby is treated as objects.SyntaxThe syntax of declaring a lambda function is shown below.lambda = lambda {}Or, we can also make use of lambda literal.lambda = ->() {} Let's first check the type of the lambda functions in Ruby with the help of a program.Example 1Consider the code shown belowsome_lambda_function = lambda { puts "Welcome to TutorialsPoint!"} puts some_lambda_function.classOutputProc Example 2Now let's create another program where we will use our own ...

Read More

Comparable module in Ruby

Mukul Latiyan
Mukul Latiyan
Updated on 25-Jan-2022 503 Views

In Ruby, the class whose objects can be ordered uses the Comparable mixin. Class definitions need to include an operator to compare receivers with each other. The operator will return either -1, 0, or 1.It returns -1 if the receiver is less than another object.If it is greater than another object, then it returns 1.It returns 0 if the receiver is equal to another object.In the Comparable module, the operator is used to implement the conventional comparison operators (*, =, and >) and sometimes also between? method as well.Now that we know a little about the comparable module in ...

Read More

Array reverse() vs reverse! in Ruby

Mukul Latiyan
Mukul Latiyan
Updated on 25-Jan-2022 993 Views

In this article, we will explore the two most widely used methods on arrays in Ruby. These are the reverse() method and the reverse! method.reverse() methodThe reverse() method in Ruby reverses the content of the array and returns a new array. Now, let's take a couple of examples to understand how it works.Example 1# reverse() method in Ruby # array declaration first_arr = [18, 22, 33, nil, 7, 6] # array declaration second_arr = [1, 5, 1, 3, 88, 9] # array declaration third_arr = [18, 22, 55, 6] # reverse method example puts "reversed array ...

Read More

How does Inheritance work in Ruby?

Mukul Latiyan
Mukul Latiyan
Updated on 25-Jan-2022 2K+ Views

Inheritance is a key aspect of any OOP language. With the help of inheritance, we can reuse the methods that are defined on the parent class (also known as superclass) in the child class (also known as subclass).In Ruby, single class inheritance is supported, which means that one class can inherit from the other class, but it can't inherit from two super classes. In order to achieve multiple inheritance, Ruby provides something called mixins that one can make use of.Inheritance helps in improving the code reusability, as the developer won't have to create the same method again that has already ...

Read More

Types of iterators in Ruby

Mukul Latiyan
Mukul Latiyan
Updated on 25-Jan-2022 242 Views

In Ruby, we have multiple types of iterators available to us. We will learn about the most common ones in this article, one by one.Each IteratorUsing this iterator, you can iterate over an array or a hash, returning each element as it is returned.Example 1Consider the code shown below# each iterator example (0..10).each do |itr|    puts itr endOutput0 1 2 3 4 5 6 7 8 9 10Times IteratorThis iterator implants a loop with a specific number of iterations. The loop starts from zero and runs until it gets one less than the specified number.Example 2# time iterator example ...

Read More

Thread life cycle and its states in Ruby

Mukul Latiyan
Mukul Latiyan
Updated on 25-Jan-2022 573 Views

In Ruby, we can create threads which have different states and a life cycle which denotes its time since it started till its ending. In this article, we will take a look at the life cycle of a thread in Ruby.Thread Life Cycle in RubyThe Thread life cycle is a brief summary of how the thread develops from the beginning to the end. With the help of Thread.new, Thread.fork, or Thread.start, a new thread can be created.A new thread does not need to be started after creation. Whenever the CPU is available, it starts automatically.A Thread object is returned by ...

Read More
Showing 301–310 of 363 articles
« Prev 1 29 30 31 32 33 37 Next »
Advertisements