
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 All Nil Elements from Array in Golang
This tutorial is all about how to remove all nil elements from the array. Basically, sometimes there are empty strings present in the array which we want to remove. Here in this tutorial, we will have a look how to remove those strings with the use of very simple methods.
Method 1: Using helper Function Approach
In this method, we will see how to remove null elements using helper function approach. The array will be taken as a parameter in the function and the output will be printed on console using print statement of Golang.
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 ? Declare a package main and import fmt in the program.
Step 2 ? Create a function named delete_empty with an array of strings as parameter from where the empty strings have to be eradicated
Step 3 ? Create an array inside the function where the non-empty values will be stored from the original array.
Step 4 ? Run a loop till the end of original array and check the condition that if the string is not equal to the empty string, then append the value of original array in the new array.
Step 5 ? Continue this Step 5 until the loop is terminated, after the iteration is completed return the arr.
Step 6 ? The array returned will be printed in the main function from where the function was called.
Step 7 ? Then execution is done using fmt.Println() function which is the print statement in Golang. Here ln means the next line in continuation.
Example
Golang program to remove all the nil elements from an array using helper function approach
package main import ( "fmt" ) func delete_empty(strarr []string) []string { var arr []string for _, str := range strarr { if str != "" { arr = append(arr, str) } } return arr } func main() { strarr := []string{"Abc", "Xyz", ""} fmt.Println("The array before removal of empty element is:", strarr) fmt.Println("The array after removal of empty element is:") fmt.Println(delete_empty(strarr)) }
Output
The array before removal of empty element is: [Abc Xyz ] The array after removal of empty element is: [Abc Xyz]
Method 2: Using the Pointer Approach
In this method, we will se how to remove null elements using pointers and json data. Here the two arrays are created where one is pointing to the other. In the end arr which referenced to arr2. The output is printed using fmt.Println() function which is a print statement in Golang.
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 ? Declare a package main and import fmt package in the program and jsondata array of type string.
Step 2 ? Create a function with an arr pointing to json data and a delete function with a parameter of string which is being called from main.
Step 3 ? In the function declare arr2 with jsondata, the value that will be stored in arr2 will be referenced by arr and in the end arr will be printed on console.
Step 4 ? Run a loop till the length of arr and check if the string is not equal to selector append the string in arr2.
Step 5 ? After the loop is terminated arr will be referencing arr2 via pointer and from the main the output will be printed using fmt.Println() function with arr in parenthesis.
Example
Golang program to remove all the nil elements from an array using pointer approach
package main import "fmt" type jsondata []string func (arr *jsondata) delete(selector string) { var arr2 jsondata for _, str := range *arr { if str != selector { arr2 = append(arr2, str) } } *arr = arr2 } func main() { arr := jsondata{"foo", "", "bar", ""} fmt.Println("The array before deletion is:") fmt.Println(arr) arr.delete("") fmt.Println("The array after deletion is:") fmt.Printf("%q", arr) }
Output
The array before deletion is: [foo bar ] The array after deletion is: ["foo" "bar"]
Conclusion
We executed the program of deleting null elements from array using two methods. In the first method we used the helper function approach to delete the element of array and in the second example we used the pointer approach in the external function in which the array as a pointer and the selector were passed as parameters. Similar outputs are obtained in both cases. Hence program executed successfully.