len

The built-in Go function that returns how many items a string, slice, map, array or channel holds.

In more detail

len of a nil slice or nil map is 0, so you can check emptiness without a nil check. For a string it counts bytes, not characters. Checking len(os.Args) before indexing avoids an index-out-of-range panic.

Example

if len(args) < 2 { os.Exit(2) }

Related terms

Taught in

References