这个调用是如何调用的?

How is this invocation called?

在 Hugo 模板中,我知道您可以使用 function param:

调用函数
{{ singularize "cats" }}

但在文档中,我还看到您也可以这样做:

{{ "cats" | singularize }}

我从未遇到过这种调用函数的方式(在 Ruby/Python 等语言中)。这是 Go 特有的,还是 Hugo 特有的?这种调用函数的方式是怎么调用的呢?另外,如果你有不止一种类型的参数,你能做到吗?

这是 Go 模板引擎的一个特性,虽然这不是一个新想法,如果你使用 unix 系统,你可以在 shell 命令中做同样的事情(想想 ls |more) .

它叫做 "chaining":您指定一个命令序列,每个命令的输出用作链中下一个命令的输入。

记录在 text/template:

A pipeline may be "chained" by separating a sequence of commands with pipeline characters '|'. In a chained pipeline, the result of each command is passed as the last argument of the following command. The output of the final command in the pipeline is the value of the pipeline.

Go 模板引擎只允许您使用单个 return 值注册和调用函数和方法;或 2 个 return 值,其中第二个值必须是 error 类型(检查以判断调用是否被认为是成功的,非 nil 错误终止模板执行并出现错误).因此,您不能链接具有多个 return 值的命令,也不能指定元组将多个值传递给具有多个参数的函数。

有关管道的更多信息,请参阅