- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 remove all elements from an array
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 add. The function then returns the final slice of array containing all the values.
Method 1: Clear the elements in an array using nil
In this method, we will set the elements of an array equal to nil which implies that the elements will be removed from the array and an empty array will be printed on the screen using the print statement in Go language.
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 the same function create an array with different values in it using append function.
Step 3 − To remove elements from the array set the array equals to nil and print the array on console.
Step 4 − Execute the print statement using fmt.Println() function where ln means the new line.
Example
Golang program to clear the elements in an array using nil
package main import "fmt" // create a function main func main() { var array []int // create an array to store values which can be removed array = append(array, 2) // use append function to store values array = append(array, 3) array = append(array, 4) array = append(array, 8) fmt.Println("The array value before setting it nil is:", array) array = nil // assign the array equals to nil to remove elements fmt.Println("The array value after setting it nil is:") fmt.Println(array) // print the empty array here }
Output
The array value before setting it nil is: [2 3 4 8] The array value after setting it nil is: []
Method 2: Clear the elements in an array using zero length
In this method, we will set the array empty slicing the array to length of zero which means that the current elements will be removed from the array and the empty array will be printed on the screen using print statement in Go language. Let’s go through the example to get the concept crystal-clear.
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 the same function create an array with different values in it using append function.
Step 3 − To remove elements from the array set the array equals to slicing zero length on console.
Step 4 − Execute the print statement using fmt.Println() function where ln means the new line.
Example
Golang program to clear the elements in an array using zero length
package main import "fmt" // create a function main to execute the program func main() { var array []int // create an empty array array = append(array, 2) // use append function to fill it with values array = append(array, 3) array = append(array, 4) array = append(array, 8) fmt.Println("The array value before setting it to nil is:", array) array = array[:0] // slice with 0 length fmt.Println("The array value after setting it to nil is:", array) fmt.Println(array) // print the empty array }
Output
The array value before setting it to nil is: [2 3 4 8] The array value after setting it to nil is: [] []
Conclusion
We executed the program of removing elements from array using two approaches. In the very first example, we have assigned nil to the array and the array is printed empty. In the second case we used the slicing of zero length to empty array elements. Both the output displayed similar results. Hence, the program executed successfully.
- Related Articles
- Golang Program To Remove Duplicate Elements From An Array
- Golang Program to Remove Repeated Elements From an Array
- Golang Program to remove all 'nil' elements from the array
- Golang Program To Remove Duplicates From An Array
- Swift Program to Remove All the Elements from the Array
- Golang Program To Remove All Occurrences Of An Element In An Array
- Swift Program to Remove Duplicate Elements From an Array
- Python Program to Remove Duplicate Elements From an Array
- Swift Program to remove all 'nil' elements from the array
- Golang program to remove elements from the linked list
- Golang program to fetch elements from an array based on an index
- Golang program to remove all whitespaces from a string
- Golang program to remove all items from hash collection
- Golang Program to remove given element from the array
- Swift Program to Remove all the elements from Dictionary
