In Go, you can use the len
function to get the length of an array and check if it's not empty.
In this example,
numbers
with some elements.len
function and comparing it with 0, and store the result in the variable notEmpty
.package main
import "fmt"
func main() {
numbers := []int{1, 2, 3}
notEmpty := len(numbers) != 0
fmt.Printf("Array is not empty: %t\n", notEmpty)
}
Array is not empty: true
In this example,
emptyArray
.len
function and comparing it with 0, and store the result in the variable notEmpty
.package main
import "fmt"
func main() {
emptyArray := []int{}
notEmpty := len(emptyArray) != 0
fmt.Printf("Array is not empty: %t\n", notEmpty)
}
Array is not empty: false
In this tutorial, we learned How to check if an Array is Not Empty in Go language with well detailed examples.