思源笔记社区正在搭建中,现邀您共建
SiYuan Community is under construction. Join us to co-build.
$myList := list 1 2 3 4 5[1 2 3 4 5]。first,mustFirstfirst。first $myList 返回 1first 会产生 panic,而 mustFirst 会返回错误到模板引擎。rest,mustRestrest。rest $myList 返回 [2 3 4 5]rest 会产生 panic,而 mustRest 会返回错误到模板引擎。last,mustLastlast:last $myList 返回 5。这基本上相当于反转列表然后调用 first。initial,mustInitialinitial。initial $myList 返回 [1 2 3 4]。initial 会产生 panic,而 mustInitial 会返回错误到模板引擎。append,mustAppend$new = append $myList 6$new 设置为 [1 2 3 4 5 6]。$myList 将保持不变。append 会产生 panic,而 mustAppend 会返回错误到模板引擎。prepend,mustPrependprepend $myList 0[0 1 2 3 4 5]。$myList 将保持不变。prepend 会产生 panic,而 mustPrepend 会返回错误到模板引擎。concatconcat $myList ( list 6 7 ) ( list 8 )[1 2 3 4 5 6 7 8]。$myList 将保持不变。reverse,mustReversereverse $myList[5 4 3 2 1]。reverse 会产生 panic,而 mustReverse 会返回错误到模板引擎。uniq,mustUniqlist 1 1 1 2 | uniq[1 2]uniq 会产生 panic,而 mustUniq 会返回错误到模板引擎。without,mustWithoutwithout 函数从列表中过滤掉项目。without $myList 3[1 2 4 5]without 可以使用多个过滤器:without $myList 1 3 5[2 4]without 会产生 panic,而 mustWithout 会返回错误到模板引擎。has,mustHashas 4 $myListtrue,而 has "hello" $myList 将返回 false。has 会产生 panic,而 mustHas 会返回错误到模板引擎。compact,mustCompact$list := list 1 "a" "foo" ""
$copy := compact $listcompact 将返回一个新列表,其中删除了空项(即“”)。compact 会产生 panic,而 mustCompact 会返回错误到模板引擎。slice,mustSliceslice list [n] [m]。它相当于 list[n:m]。slice $myList 返回 [1 2 3 4 5]。它与 myList[:] 相同。slice $myList 3 返回 [4 5]。它与 myList[3:] 相同。slice $myList 1 3 返回 [2 3]。它与 myList[1:3] 相同。slice $myList 0 3 返回 [1 2 3]。它与 myList[:3] 相同。slice 会产生 panic,而 mustSlice 会返回错误到模板引擎。chunkchunk size list。这对于分页很有用。chunk 3 (list 1 2 3 4 5 6 7 8)[ [ 1 2 3 ] [ 4 5 6 ] [ 7 8 ] ]。[]interface{} 实现的。对于嵌入 Sprig 的 Go 开发人员,您可以将 []interface{} 项传递到模板上下文中,并能够在这些项上使用所有 list 函数。