Found 1082 Articles for Go Programming

Golang program to count the elements of a slice

Akhil Sharma
Updated on 23-Jan-2023 14:52:16

979 Views

In this article, we will learn how to count the elements of a slice using a different set of examples. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas a slice is a dynamic array, meaning its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover; they are passed by reference instead by value. Let us learn through examples how it can be executed. Syntax func append(slice, element_1, element_2…, element_N) []T The append function is used to add values to ... Read More

Golang to remove null from a slice

Akhil Sharma
Updated on 23-Jan-2023 14:48:17

1K+ Views

In this article, we will learn how to remove a null value from a slice using a variety of examples. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas a slice is a dynamic array, meaning its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover; they are passed by reference instead of value. Let us learn through examples how it can be executed. Method 1: Using For Loop In this method, we will see how to remove a null value ... Read More

Golang program to calculate the sum of right diagonal elements

Akhil Sharma
Updated on 23-Jan-2023 14:44:46

173 Views

In this article, we will learn how to calculate the sum of right diagonal matrix with the help of different examples. A matrix is a 2-d array. The output will be printed on the screen using fmt.println() function which is a print statement in Golang. 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 function main, in that function create a matrix, and fill with some values inside it. Step 3 − Print the ... Read More

Golang program to calculate the sum of left diagonal matrix

Akhil Sharma
Updated on 23-Jan-2023 14:41:36

189 Views

In this article, we will learn how to calculate the sum of left diagonal matrix with the help of different examples. A matrix is a 2-d array. The left diagonal matrix is printed using the logic of indexes equal to each other. The output will be printed on the screen using fmt.println() function which is a print statement in Golang. 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 function main and in that ... Read More

Golang program to print the left diagonal matrix

Akhil Sharma
Updated on 23-Jan-2023 14:39:06

209 Views

In this article, we will see how to print the left diagonal matrix with the help of suitable examples. A matrix is a 2-d array. Here in the examples, we will use a nested for loop which will iterate through the rows and columns of the matrix to print the left diagonal matrix. The output will be printed on the screen using fmt.println() function which is a print statement in Golang. 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 ... Read More

Golang program to print right diagonal matrix

Akhil Sharma
Updated on 23-Jan-2023 14:36:40

250 Views

In this article, we will see how to print the right diagonal matrix with the help of suitable examples. A matrix is a 2-d array. Here in the examples, we will use a nested for loop which will iterate through the rows and columns of the matrix to print the right diagonal matrix. The output will be printed on the screen using fmt.println() function which is a print statement in Golang. 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 ... Read More

Golang program to initialize a slice

Akhil Sharma
Updated on 23-Jan-2023 14:34:56

2K+ Views

In this article, we will learn how to initialize a slice using a variety of ways in examples. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas a slice is a dynamic array, meaning its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover they are passed by reference instead by value. Let’s learn through examples how it can be executed. Algorithm Step 1 − Create a package main and declare fmt(format package) package in the program where main produces ... Read More

Golang program to remove all elements from an array

Akhil Sharma
Updated on 17-Jan-2023 12:35:42

4K+ Views

In this tutorial, we will execute the program of removing all the elements of array using different set of examples. An array is a well-defined collection of elements stored at contiguous memory locations. The output is printed in the screen using fmt.Println() function. Let’s see few examples to get a clear understanding of this concept. Syntax func append(slice, element_1, element_2…, element_N) []T The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to ... Read More

Golang program to get the first and last element from a slice

Akhil Sharma
Updated on 17-Jan-2023 12:32:32

1K+ Views

In this tutorial, we will discuss how to obtain the first and last element from a slice. A slice is a dynamic-sequence that stores elements of a similar type. It is similar to array but it can be resized whereas an array is of fixed range. Let’s understand the concept using some examples. Method 1: Using integer values In this method, we will use integer values to get the first and last element from a slice. The output will be printed on the screen using fmt.Println() function. Let’s go through the example to understand how it can be done. Algorithm ... Read More

Golang program to compare elements in two slices

Akhil Sharma
Updated on 17-Jan-2023 12:29:33

3K+ Views

In this tutorial, we will learn how to compare elements in two slices. In slices a simple equality comparison is not possible so the slices are compared with their lengths and the elements present in the loop. The output will be printed in the form of Boolean value on the console with the help of fmt.Println() function. Let’s see how to execute this with the help of an example. Method 1: Using a user-defined function In this method, we will compare elements in two slices using an external function and, in that function, we will set some conditions, if the ... Read More

Advertisements