OneBite.Dev - Coding blog in a bite size

check if array is empty in Go

Code snippet on how to check if array is empty in Go

func isEmpty(arr []string) bool {
  	if len(arr) == 0 {
    	return true
  	}
  	return false
}

This code checks if an array is empty and returns a boolean value of either true or false. The function takes an array of strings as an argument and checks if the length of the array is equal to 0. If it is, the function returns true, otherwise it returns false. The len() function takes an array as an argument and returns the number of elements contained in the array. In this example, if the array has a length of 0, that indicates that the array is empty and the function returns true. If the array has a length of any value other than 0, then the function returns false.

go