collections.Apply

apply 函數接收三個或更多參數,具體數量取決於應用於集合元素的函數。

第一個參數是集合本身,第二個參數是函數名稱,其餘的參數則會傳遞給該函數,其中字串 "." 表示集合的元素。

{{ $s := slice "hello" "world" }}

{{ $s = apply $s "strings.FirstUpper" "." }}
{{ $s }} → [Hello World]

{{ $s = apply $s "strings.Replace" "." "l" "_" }}
{{ $s }} → [He__o Wor_d]