duanla4959 2018-12-18 14:13
浏览 39
已采纳

该调用如何称呼?

Inside Hugo templates, I'm aware that you can call a function using function param:

{{ singularize "cats" }}

but in the documentation, I'm also seeing you can also do:

{{ "cats" | singularize }}

I've never encountered this way of calling functions (inside languages like Ruby/Python). Is this Go-specific, or just Hugo-specific? How is this way of calling a function called? Also, can you do it if you have more than 1 type of argument?

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-12-18 14:23
    关注

    That is a feature of the Go template engine, although it's not a new idea, if you used unix systems, you can do the same in shell commands (think of e.g. ls |more).

    It's called "chaining": you specify a sequence of commands, and the output of each is used as the input of the next in the chain.

    It's documented at 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.

    The Go template engine only allows you to register and call functions and methods with a single return value; or 2 return values of which the second must be of type error (which is checked to tell if the call is considered successful, and non-nil errors terminate the template execution with an error). So you can't chain commands that have multiple return values, and you can't specify tuples to pass multiple values to functions having multiple parameters.

    For more about pipelines, see golang template engine pipelines

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 STM32驱动继电器