如果-否则条件

huangapple go评论84阅读模式
英文:

If - Else condition

问题

我对根据一些条件向GO模板添加内容很感兴趣。我有一个定义如下的结构体 -

{

    "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",
    },

}

我需要遍历"resourceActions",如果其中定义的操作如update、restart等的输入为null,则生成"A()",否则生成A(input *{inputVAL})

示例 -

{

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

}

如何在GO模板中实现这个功能?

英文:

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

得分: 2

我像这样解决了我的问题 -

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

这在GO模板中运行良好。

英文:

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.

huangapple
  • 本文由 发表于 2014年12月4日 03:35:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/27280158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定