Found 1082 Articles for Go Programming

How to count specific characters present in the slice in Golang?

Siva Sai
Updated on 05-May-2023 10:23:58

550 Views

In Golang, counting specific characters in a slice can be done by iterating over the slice and comparing each character with the target character. There are multiple ways to count the occurrence of a specific character in a slice, such as using a loop, a map, or the strings.Count function. In this article, we will discuss each method with examples to help you understand how to count specific characters present in a slice in Golang. Using a Loop One way to count the occurrence of a specific character in a slice is by iterating over the slice and checking each ... Read More

How to Copy Struct Type Using Value and Pointer Reference in Golang?

Siva Sai
Updated on 05-May-2023 10:22:41

5K+ Views

In Go, you can copy a struct by value or by reference using pointers. When you copy a struct by value, a new copy of the struct is created in memory, and all the fields of the original struct are copied to the new one. On the other hand, when you copy a struct by reference using a pointer, both the original and the copied struct share the same memory address. In this article, we will discuss how to copy a struct type in Go using value and pointer reference. Copy Struct Using Value Reference To copy a struct using ... Read More

How to copy one slice into another slice in Golang?

Siva Sai
Updated on 05-May-2023 10:21:45

2K+ Views

In Golang, slices are a powerful and flexible data structure that allows you to store a sequence of elements of the same type. When working with slices, you may need to copy one slice into another slice. Fortunately, Golang provides an easy and efficient way to do this. In this article, we will discuss how to copy one slice into another slice in Golang. Using The Copy Function in Golang Golang provides a built-in copy function that allows you to copy the elements of one slice into another slice. The copy function takes two arguments: the destination slice and the ... Read More

How to copy a map to another map in Golang?

Siva Sai
Updated on 05-May-2023 10:19:41

5K+ Views

Maps are an important data structure in Golang that stores key-value pairs. Sometimes it is required to copy one map to another map. Copying a map in Golang can be done using different approaches. In this article, we will discuss some of the methods to copy a map to another map in Golang. Method 1: Using a For Loop One way to copy a map to another map is to use a for loop. Example Here is an example − package main import "fmt" func main() { map1 := map[string]int{"a": 1, "b": 2, "c": 3} ... Read More

How to Convert string to float type in Golang?

Siva Sai
Updated on 05-May-2023 10:11:40

5K+ Views

In Go, we can convert a string to a float type using the strconv package. The strconv package provides the ParseFloat function to convert a string to a float type. This function takes three arguments - the string to be converted, the bit size of the float type, and the precision of the float type. In this article, we will discuss how to convert a string to a float type in Go. Syntax The syntax of the ParseFloat function is as follows − func ParseFloat(s string, bitSize int) (float64, error) The ParseFloat function takes two arguments - the string ... Read More

How to convert Int data type to Float in Golang?

Siva Sai
Updated on 05-May-2023 10:09:55

3K+ Views

In Golang, it is easy to convert one data type to another using type conversion. In this tutorial, we will explore how to convert an integer to a float data type. Converting Int to Float In Golang, converting an integer to a float is a straightforward process. You can use the float64() function to convert an integer to a float data type. Example Here is an example − package main import "fmt" func main() { i := 5 f := float64(i) fmt.Println("The integer value is: ", i) ... Read More

Golang program to return the values from the block

Akhil Sharma
Updated on 03-May-2023 13:04:56

124 Views

In this golang article, we will learn how to return the values from the block using external function, an anonymous function. A block is created using curly braces where the scope of variables remains inside the block and not outside of it. Example 1 In this Example, two values of x and y are added in the code block and z will be assigned the added value. Then, call the function getResult in which another x and y are added and assigned to z in the code block. package main import "fmt" func main() { ... Read More

Golang program to convert milliseconds to minutes and seconds

Akhil Sharma
Updated on 03-May-2023 13:03:39

1K+ Views

In this golang article, we will write Go language programs to convert milliseconds to minutes and seconds using various methods. Milliseconds are converted to minutes and seconds mathematically using different set of logics. Algorithm Step 1 − Import the required packages in the program Step 2 − Create a main function Step 3 − In the main take milliseconds value and convert it into minutes and seconds using different logics mathematically Step 4 − Then, print the output using Printf function from the fmt package Example 1 In this Example, we will take the milliseconds value. Then, the ... Read More

Golang program to create multiple BEGIN and END blocks

Akhil Sharma
Updated on 03-May-2023 13:02:46

71 Views

In this article, we will learn to write Go language programs that will create multiple BEGIN and END blocks using curly braces, “conditional statements”, as well as “external functions”. A block is created using curly braces, the scope of a variable remains inside the block and not outside it. Algorithm Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output. Step 2 − Create a main function Step 3 − In the main, create a first block by initializing ... Read More

Golang program to demonstrate BEGIN and END blocks

Akhil Sharma
Updated on 03-May-2023 13:01:43

110 Views

In this Go language article, we will write programs to demonstrate BEGIN and END blocks using sum of numbers, iteration and two variables. Blocks are used to group the statements where the variables described in the block can only be used in that scope and not outside it. They also help in code readability Algorithm Step 1 − Create a package main and declare fmt(format package) package in the program where main produces executable codes and fmt helps in formatting input and output. Step 2 − Create a main function and in this function create two begin and end ... Read More

Advertisements