英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论