Home  >  Article  >  Backend Development  >  How to determine whether an array is empty in golang

How to determine whether an array is empty in golang

尚
Original
2019-12-31 13:57:5919812browse

How to determine whether an array is empty in golang

#go languageAn array is a collection of elements of the same type. For example, the set of integers 5, 8, 9, 79, 76 forms an array. Go does not allow mixing elements of different types (such as integers and strings) in arrays.

Golang determines whether the array is empty:

package main
 
import "fmt"
 
func main() {
	var arr []string
	
	if arr == nil {
		fmt.Println("this is null")
	}
 
	if len(arr) > 0 {
		fmt.Println("len arr > 0")
	}else{
		fmt.Println("len this is null")
	}
	
	if arr[0] != "" {
		fmt.Println("arr 0 != null")	
	}else{
		fmt.Println("[0] this is null")
	}
 
}

In the go language nil is a frequently used and important predefined identifier. It is a zero-valued representation of many types.

len() can be used to check the length of an array or slice.

For more golang knowledge, please pay attention to the golang tutorial column.

The above is the detailed content of How to determine whether an array is empty in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is const in golang?Next article:What is const in golang?