douwu5428 2014-12-03 19:35
浏览 4
已采纳

如果-其他条件

I am interested in adding content to a GO template based on some conditions. I have a struct defined like this -

{

    "resourceActions": {
    "update": {
    "input": null,
    "output": "instance",
    },
    "stop": {
    "input": "instanceStop",
    "output": "instance",
    },
    "console": {
    "input": "instanceConsoleInput",
    "output": "instanceConsole",
    },
    "restart": {
    "input": null,
    "output": "instance",
    },
    "remove": {
    "input": null,
    "output": "instance",
    },

}

I need to iterate over "resourceActions" and if the action defined within that like update, restart etc has input as null then generate "A()" else generate A(input *{inputVAL})

Example -

{

    for Update -  A()
    for stop -  A(input *instanceStop)
    for console - A(input *instanceConsoleInput)
    for restart - A()

}

How can I do this in GO-tempaltes

  • 写回答

1条回答 默认 最新

  • dougua9328 2014-12-03 21:09
    关注

    I solved my issue like this -

    {
        {{ $temp := .schema.Id }}
        {{if .Input}} func (c *Container) {{$key }}(input *{{.Input}}) *{{$temp}}{} {{else}} func (c *Container) {{$key}}() *{{$temp}}{}{{end}}
    }
    

    This works fine in GO templates.

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

报告相同问题?

悬赏问题

  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题