drtzb06222 2018-05-10 09:46
浏览 163

如何将golang变量设置为模板变量

I am new to golang and want to understand how to assign a template variable in golang using the golang variable.

I am using go-swagger to generate go code.

Below is the piece of a customized template to generate my swagger client.

func demo() {
 {{range .Operations}}
   Value := main.CheckAvail(*{{.Package}}.{{ pascalize .Name }})
   {{$value := .Value}}
   {{if $value }}
      {{ pascalize .Name }}
   {{end}}
 {{end}}
}

But it gives me the error that:

" <.Value>: can't evaluate field Value in type generator.GenOperation ".

How do I assign the value to the $value? Any help?

  • 写回答

1条回答 默认 最新

  • dongtuo7364 2018-05-11 06:30
    关注

    This is not possible. The template file is a wire frame for a go file. It just generates a file of go code, which is not executed yet.

    All in {{ ... }} is evaluated, when the template is parsed. At this point, all text outside the curly braces is just plain text. After the (successful) execution of the template generation the generated text will be picked up by the go compiler and compiled (if it is correct go code).

    In this case you cannot evaluate Value.

    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来