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.

Updated on: 17-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements