
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Elements of a Slice in Go
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 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 add. The function then returns the final slice of array containing all the values.
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 ? Initialize a slice and fill it with some values, which are to be counted using, the append function.
Step 3 ? Print the slice created on the console using the print statement
Step 4 ? Count the elements of the slice using the length function denoted by len
Step 5 ? Here range is used in for loop instead of blank space to include the index in the slice.
Step 6 ? Print the length of the slice created on the console using the print statement
Step 7 ? The print statement is executed using fmt.Println() function where ln means new line.
Using Len Method
In this example, we will see how to count the elements of a slice using the len method. len is used to calculate the length of the slice. Let's see through the algorithm and the code how it's being done.
Example
package main import "fmt" func main() { var slice []int // create a slice slice = append(slice, 10) //fill the elements in slice using append function slice = append(slice, 20) slice = append(slice, 30) length_slice := len(slice) //calculate length of slice using len method fmt.Println("The elements of slice are:", slice) //print slice elements fmt.Println("The slice has", length_slice, "elements.") //print length of slice }
Output
The elements of slice are: [10 20 30] The slice has 3 elements.
Using For Loop and Count Variable
In this example, we will see how to count the elements of a slice using for loop. For loop will be used to calculate the length of slice by adding elements on each iteration. Let's see through the algorithm and the code how it's being done.
Example
package main import "fmt" func main() { var slice []int slice = append(slice, 10) //fill the elements in slice using append function slice = append(slice, 20) slice = append(slice, 30) fmt.Println("The elements in slice are:", slice) //print slice elements count := 0 //count to store number of elements in slice for range slice { //for loop runs till length of slice count++ } fmt.Println("The slice has", count, "elements.") //print the length of slice }
Output
The elements in slice are: [10 20 30] The slice has 3 elements.
Conclusion
We executed the program of counting the elements of a slice in the above 2 examples. In the first example, we used len method to count the elements of the slice and in the second example, we used the for loop to count the elements of the slice on every iteration. In both examples, we used the append function to add elements to the slice which shortens the code.