Go provides several built-in functions to work with slices. You’ve already seen the built-in len function when looking at arrays. It works for slices too. Passing a nil slice to len returns 0.
Note
Functions like
lenare built into Go because they can do things that can’t be done by the functions that you can write. You’ve already seen thatlen’s parameter can be any type of array or any type of slice. You’ll soon see that it also works for strings and maps. In the Channels section, you’ll see it working with channels. Trying to pass a variable of any other type tolenis a compile-time error. Go doesn’t let developers write a function that accepts any string, array, slice, channel, or map, but rejects other types.
Progress is saved on this device.