| 1 |
--- |
| 2 |
title: collections.Apply |
| 3 |
description: Returns a slice by transforming each element of the given slice using a specific function and parameters. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [apply] |
| 9 |
returnType: '[]any' |
| 10 |
signatures: [collections.Apply SLICE FUNCTION PARAM...] |
| 11 |
aliases: [/functions/apply] |
| 12 |
--- |
| 13 |
|
| 14 |
The `apply` function takes three or more arguments, depending on the function being applied to the slice elements. |
| 15 |
|
| 16 |
The first argument is the slice itself, the second argument is the function name, and the remaining arguments are passed to the function, with the string `"."` representing the slice element. |
| 17 |
|
| 18 |
```go-html-template |
| 19 |
{{ $s := slice "hello" "world" }} |
| 20 |
|
| 21 |
{{ $s = apply $s "strings.FirstUpper" "." }} |
| 22 |
{{ $s }} → [Hello World] |
| 23 |
|
| 24 |
{{ $s = apply $s "strings.Replace" "." "l" "_" }} |
| 25 |
{{ $s }} → [He__o Wor_d] |
| 26 |
``` |