2019-10-21 04:22:28 -04:00
|
|
|
---
|
|
|
|
title: len
|
2023-05-22 10:43:12 -04:00
|
|
|
description: Returns the length of a string, slice, map, or collection.
|
2019-10-21 04:22:28 -04:00
|
|
|
categories: [functions]
|
2023-10-20 03:42:39 -04:00
|
|
|
keywords: []
|
2020-05-18 09:24:58 -04:00
|
|
|
menu:
|
|
|
|
docs:
|
2023-05-22 10:43:12 -04:00
|
|
|
parent: functions
|
2023-10-20 03:42:39 -04:00
|
|
|
function:
|
|
|
|
aliases: []
|
|
|
|
returnType: int
|
|
|
|
signatures: [len INPUT]
|
|
|
|
relatedFunctions:
|
|
|
|
- len
|
|
|
|
- strings.Count
|
|
|
|
- strings.CountRunes
|
|
|
|
- strings.CountWords
|
|
|
|
- strings.RuneCount
|
|
|
|
aliases: [/functions/len]
|
2019-10-21 04:22:28 -04:00
|
|
|
---
|
|
|
|
|
2023-10-20 03:42:39 -04:00
|
|
|
{{% readfile file="/functions/_common/go-template-functions.md" %}}
|
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
With a string:
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
```go-html-template
|
|
|
|
{{ "ab" | len }} → 2
|
|
|
|
{{ "" | len }} → 0
|
|
|
|
```
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
With a slice:
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
```go-html-template
|
|
|
|
{{ slice "a" "b" | len }} → 2
|
|
|
|
{{ slice | len }} → 0
|
|
|
|
```
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
With a map:
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
```go-html-template
|
|
|
|
{{ dict "a" 1 "b" 2 | len }} → 2
|
|
|
|
{{ dict | len }} → 0
|
|
|
|
```
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
With a collection:
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
```go-html-template
|
|
|
|
{{ site.RegularPages | len }} → 42
|
|
|
|
```
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
You may also determine the number of pages in a collection with:
|
2019-10-21 04:22:28 -04:00
|
|
|
|
2023-05-22 10:43:12 -04:00
|
|
|
```go-html-template
|
|
|
|
{{ site.RegularPages.Len }} → 42
|
|
|
|
```
|