Go Programming Articles

Page 85 of 86

Empty Slice vs. Nil Slice in Golang

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 1K+ Views

In this article, we will see the differences and similarities that are there between a slice that is declared as an empty and a nil slice.Slices in Golang are used to store a sequence of elements. Slices can be expanded at any time and they are declared in the same way as an array, but without their capacity defined.Nil SliceA nil slice is a slice in which while declaring, we simply define the name and the data type and not make use of the built-in make function.A nil slice is a slice that has nil as its zero value and ...

Read More

Functions vs Methods in Golang

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 6K+ 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

Channel synchronization in Golang

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 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

How to get the Response status code in Golang?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 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

How to detect the content type of a file in Golang?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 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

How to decode JSON into objects in Golang?

Kiran Kumar Panigrahi
Kiran Kumar Panigrahi
Updated on 01-Nov-2021 1K+ Views

Suppose we have a JSON that looks like this.{ "name":"Mukul Latiyan", "age":10, "sports":[ "football", "tennis", "cricket" ] }Now, we want to convert this JSON into struct fields which we can access later and maybe iterate over too.In order to do that, we need to first make a struct that will satisfy the fields of the above JSON.The struct shown below will work just fine for the above JSON.type Person struct ...

Read More

Golang Program to find the position of the rightmost set bit

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 17-Mar-2021 373 Views

ExamplesConsider n = 20(00010100)Now return log2(20 & -20) => 2+1 => 3Approach to solve this problemStep 1 − Define a method, where n and is an argument, return type is int.Step 2 − Return log2(n & -n)+1.Examplepackage main import (    "fmt"    "math"    "strconv" ) func FindRightMostSetBit(n int) int {    if (n & 1) != 0{       return 1    }    return int(math.Log2(float64(n & -n))) + 1 } func main(){    var n = 20    fmt.Printf("Binary of %d is: %s.", n, strconv.FormatInt(int64(n), 2))    fmt.Printf("Position of the rightmost set bit of the given number %d is %d.", n, FindRightMostSetBit(n)) }OutputBinary of 20 is: 10100. Position of the rightmost set bit of the given number 20 is 3.

Read More

Golang Program to toggle the Kth of the given number n.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 17-Mar-2021 199 Views

ExamplesConsider n = 20(00010100), k = 3.After toggling the kth bit of the given number: 00010000 => 16.Approach to solve this problemStep 1 − Define a method, where n and k would be the arguments, returns type is int.Step 2 − Perform AND operation with n ^ (1

Read More

Golang program to check if k'th bit is set for a given number or not.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 17-Mar-2021 2K+ Views

ExamplesConsider n = 20(00010100), k = 3So, the result after turning off the 3rd bit => 00010000 & (1

Read More

Golang Program to turn on the k'th bit in a number.

Rishikesh Kumar Rishi
Rishikesh Kumar Rishi
Updated on 17-Mar-2021 236 Views

Example For example consider n = 20(00010100), k = 4. So result after turning on 4th bit => 00010000 | (1

Read More
Showing 841–850 of 852 articles
Advertisements