Mukul Latiyan

Mukul Latiyan

363 Articles Published

Articles by Mukul Latiyan

Page 22 of 37

How to use iota in Golang?

Mukul Latiyan
Mukul Latiyan
Updated on 22-Feb-2022 5K+ Views

Iota in Go is used to represent constant increasing sequences. When repeated in a constant, its value gets incremented after each specification. In this article, we will explore the different ways in which we can use iota in Go.Let's first consider a very basic example, where we will declare multiple constants and use iota.Example 1Consider the code shown belowpackage main import (    "fmt" ) const (    first = iota    second = iota    third = iota ) func main() {    fmt.Println(first, second, third) }OutputIf we run the command go run main.go, then we will get the ...

Read More

Anonymous goroutines in Golang

Mukul Latiyan
Mukul Latiyan
Updated on 22-Feb-2022 5K+ Views

In order to be able to understand the anonymous goroutines, we must be aware of the existence of anonymous functions and goroutines. We will first explore the anonymous functions that are the real reason behind the motivation of anonymous goroutines and then we will learn a little about what goroutines are, before finally checking a few examples of anonymous goroutines.Anonymous functionsIn Golang, 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 ...

Read More

How to check if a string contains a substring in Golang?

Mukul Latiyan
Mukul Latiyan
Updated on 21-Feb-2022 970 Views

We know that substrings are a contiguous sequence of characters in a string, and in order to check whether a string contains a substring, we have two options available.The first approach is to use a built-in function called Contains() and the second approach is to make use a self-written logic for the same.The syntax for the Contains() function of the strings package is shown below.func Contains(s, substr string) boolIn the above syntax, there are two parameters inside the function Contains(). The first parameter is the string in which we are trying to find the pattern, and the second is the ...

Read More

How to check if a key exists in a map in Golang?

Mukul Latiyan
Mukul Latiyan
Updated on 21-Feb-2022 955 Views

We know that maps in Go contain key-value pairs. There are often instances where we would want to know that a certain key exists in a map or not, in such cases, we have two options available.The first approach is very naive, as in this approach, we basically iterate over the map with the help of the range clause and then compare each key to the key that we want to check if is available or not.And the second approach is a bit better, as we make use of the if statement with a bit of syntactic sugar.Let's first check ...

Read More

How to check if a slice contains an element in Golang?

Mukul Latiyan
Mukul Latiyan
Updated on 21-Feb-2022 4K+ Views

Many languages do provide a method similar to indexOf() where one can find the existence of a particular element in an array-like data structure. However, in Golang, there's no such method and we can simply implement it with the help of a for-range loop.Let's suppose we have a slice of strings, and we want to find out whether a particular string exists in the slice or not.Example 1Consider the code shown below.package main import (    "fmt" ) func Contains(sl []string, name string) bool {    for _, value := range sl {       if value == name ...

Read More

Number Parsing in Golang

Mukul Latiyan
Mukul Latiyan
Updated on 21-Feb-2022 840 Views

Number parsing in Go is about converting the numbers that are present in string form to number form. By number form, we mean that these numbers can either be converted into integers, floats, etc.The most widely used package for number parsing is the "strconv" package that Go library provides us with. There are many cases that are present in the number parsing in Go, we will talk about all of these one by one in this article.The most basic approach is when we have a base 10 number that is actually present in a string form and we want to ...

Read More

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
Showing 211–220 of 363 articles
« Prev 1 20 21 22 23 24 37 Next »
Advertisements