Golang to check if a slice is empty


In this article, we will check if the slice is empty or not 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 by value. Let’s learn through examples how it can be executed.

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 function main and in that function initialize a slice and fill values using append function.

  • Step 3 − Print the slice on the console using print statement in Golang.

  • Step 4 − Check a condition that if length of slice is equals to 0 print on the console that the slice is empty else print slice is not empty.

  • Step 5 − The print statement is executed using fmt.Println() function where ln means new line.

Using Len Method

In this example, we will see how to find whether a slice is empty or not using len method. The len method is used to calculate the length of the slice. Here with the help of algorithm and code let us understand this example.

Example 

package main
import "fmt"

//create a function main
func main() {

	var slice []int  // initialize slice

	slice = append(slice, 1) //fill the slice using append function
	slice = append(slice, 2)
	slice = append(slice, 3)
	slice = append(slice, 4)

	fmt.Println("The slice created by user is:", slice)

	if len(slice) == 0 {
		fmt.Println("Slice is empty")  
	} else {
		fmt.Println("Slice is not empty") 
	}

}

Output

The slice created by user is: [1 2 3 4]
Slice is not empty

Using Nil Value

In this example, we will see whether a slice is empty or not by comparing it with a nil value. We will compare the slice created with nil value. Let’s understand the example with the help of algorithms and code.

Example 

package main
import "fmt"
func main() {

	var slice []int //initialize a slice
	slice = append(slice, 1)  //fill the slice using append method
	slice = append(slice, 2)
	slice = append(slice, 3)
	slice = append(slice, 4)

	fmt.Println("The slice created by user is:", slice) 

	if slice == nil {
		fmt.Println("Slice is empty") 
	} else {
		fmt.Println("Slice is not empty") 
	}
}

Output

The slice created by user is: [1 2 3 4]
Slice is not empty

Using Indexing of Slices

In this example, we will see whether a slice is empty or not by comparing the index value with zero. We will compare the slice’s index created with the zero value. Let’s understand the example with the help of algorithms and code.

Example 

package main
import "fmt"
func main() {

	var slice []int //create slice
	slice = append(slice, 1) //fill elements using append method
	slice = append(slice, 2)
	slice = append(slice, 3)
	slice = append(slice, 4)

	fmt.Println("The slice created by user is:", slice)
	if slice[0] == 0 {
		fmt.Println("Slice is empty")
	}else {
		fmt.Println("Slice is not empty") 
	}
}

Output

The slice created by user is: [1 2 3 4]
Slice is not empty

Conclusion

In the above program, we used three examples to check if a slice is empty or not. In the first example, we used the len method to check if a slice is empty. In the second example, we used nil to compare the slice and in the third example, we used indexing.

Updated on: 23-Jan-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements