
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
Remove Null from a Slice in Golang
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 from a slice using a for loop in an external function. Let's see through the algorithm and the code how it's being done.
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 ? Create a main function and in that function create a slice with some values inside that slice including null values.
Step 3 ? Create a function named removenullvalue with a slice as a parameter inside it.
Step 4 ? Create an empty slice called result and this result will be used to append the non-nil elements inside it.
Step 5 ? Run a loop till the length of the slice and in every iteration check if the element of slice is not equal to null append those elements inside the result and move to next iteration.
Step 6 ? After the loop terminates return the output slice to the function.
Step 7 ? The output slice will be printed on the console using fmt.Println() function where ln means new line.
Example
Golang program to remove null from a slice using for loop in the example.
package main import "fmt" func removenullvalue(slice []interface{}) []interface{} { var output []interface{} for _, element := range slice { if element != nil { //if condition satisfies add the elements in new slice output = append(output, element) } } return output //slice with no nil-values } func main() { slice := []interface{}{10, 20, nil, 30, nil, 40} //create slice fmt.Println("The original slice is:", slice) slice = removenullvalue(slice) fmt.Println("The slice after removal of null value is:") fmt.Println(slice) // Output: [1 2 3 4] }
Output
The original slice is: [10 2030 40] The slice after removal of null value is: [10 20 30 40]
Method 2: Using a Filter
In this example, we will see how to remove a null value from a slice using a for loop in an external function. Let's see through the algorithm and the code how it's being done.
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 ? Create main function and in that function create a slice with non-nil and nil values.
Step 3 ? Call a function named removenullelement with a slice as a parameter inside it.
Step 4 ? In the removenullelement function call the filter function with slice and filter as inputs in it.
Step 5 ? Inside the filter function, create an empty slice named output that will be used to append the elements of slice.
Step 6 ? Run a loop until the length of slice and the filter function will return a new slice that satisfy the filter.
Step 7 ? The returned slice will be obtained by removenullelement function which will use the filter function to remove all nil values from the slices and return it back to main function.
Step 8 ? The new slice will be printed on the console using fmt.Println() function where ln means new line.
Example
Golang program to remove null from a slice using filter in the example.
package main import "fmt" func removenullelement(slice []interface{}) []interface{} { return filter(slice, func(i interface{}) bool { return i != nil }) } func filter(slice []interface{}, f func(interface{}) bool) []interface{} { var output []interface{} for _, element := range slice { if f(element) { output = append(output, element) //the values that satisfy filter will be appended in the output } } return output } func main() { slice := []interface{}{1, 2, nil, 3, nil, 4} //create slice fmt.Println("The original slice is:", slice) slice = removenullelement(slice) fmt.Println("The slice after removing null element is:") fmt.Println(slice) // Output: [1 2 3 4] }
Output
The original slice is: [1 23 4] The slice after removing null element is: [1 2 3 4]
Conclusion
We executed this program of removing the nil elements from the slice using two examples. In the first method, we used for loop to remove nil elements, and in the second method; we used a filter method to remove the null values. Both examples give similar results.