
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
Golang Program to Search an Element in the Slice
In this tutorial, we will grasp how to search an element in slice using different set of examples. A slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas slice is a dynamic array which means 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.
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.
Method 1: Using an external user-defined function
In this method, we will use an external function to search elements in the slice. The slice and the element to be searched will be passed as a parameter in the function. The output will be printed on the console using fmt.Println() function. Let's see through the code how is all being done.
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 named search_ele with slice and the element to be searched as a parameter and this function is called from main.
Step 3 ? Run a loop till the length of slice and check whether the element to be searched is equal to any element of slice
Step 4 ? If its true return the index and if its not true return -1 to the custom function.
Step 5 ? Call the main function.
Step 6 ? In the main function check if the value is equal to -1 print that the element is not present in the slice else print its present in the slice.
Step 7 ? The print statement is executed using fmt.Println() function where ln stands for new line.
Example
Golang program to search an element in slice using an external function
package main import "fmt" func main() { // Declare a slice of integers var slice []int slice = append(slice, 10) // create slice using append function slice = append(slice, 20) slice = append(slice, 30) slice = append(slice, 40) slice = append(slice, 50) fmt.Println("The slice given here is:", slice) // Call the search function and store the value in a variable named val val := search_ele(slice, 40) fmt.Println("The value to be searched from the slice is:", 40) if val != -1 { fmt.Println("The element is found in slice at index:", val) } else { fmt.Println("The element was not found in the slice") } } func search_ele(slice []int, key int) int { for i, element := range slice { if element == key { // check the condition if its true return index return i } } return -1 }
Output
The slice given here is: [10 20 30 40 50] The value to be searched from the slice is: 40 The element is found in slice at index: 3
Method 2: Using main function
In this method, we will use main function to search elements in the slice. A flag will be created and its value will help us print whether the element is present in slice or not. The output will be printed on the console using fmt.Println() function. Let's see through the code how is all being done.
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 the function create a slice using append function and a variable flag of type bool with initial value as false.
Step 3 ? Create a variable item and assign it the value which is to be searched.
Step 4 ? Run a loop till the length of slice and check whether the element to be searched is equal to any element of slice.
Step 5 ? If its true set the flag as true and break the loop but if its not true run the loop till the end and after loop terminates check a condition.
Step 6 ? if the flag is true print the statement that the element is present in the slice else print that the element is not present in the slice.
Step 7 ? The print statement is executed using fmt.Println() function where ln stands for new line.
Example
Golang program to search an element in slice using main function
package main import "fmt" func main() { var slice []int slice = append(slice, 10) // create slice using append function slice = append(slice, 20) slice = append(slice, 30) slice = append(slice, 40) slice = append(slice, 50) var flag bool = false // assign initial value as false fmt.Println("The slice given here is:", slice) var item int = 8 fmt.Println("The value to be searched from the slice is:", item) for element := range slice { if element == item { flag = true // break the loop if flag is true break } } if flag { fmt.Println("The element is present in the slice") } else { fmt.Println("The element is not present in the slice") } }
Output
The slice given here is: [10 20 30 40 50] The value to be searched from the slice is: 8 The element is not present in the slice
Conclusion
We executed the program of searching the elements of a slice using two examples. In the first example we used a custom function to search the element and in the second example we used the main function to search values. Both the examples give similar output. Hence the program executed successfully.