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 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数