Go text/template: how to convert bool to int 0/1?

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

Go text/template: how to convert bool to int 0/1?

问题

使用Go的text/template语言,您可以如何将bool转换为int(false=0,true=1)?

以下是使用我自己的goproc工具(https://github.com/dolmen-go/goproc)执行命令行模板的示例:

  1. $ echo false | goproc -e '{{.}} => <template here>'
  2. false => 0
  3. $ echo true | goproc -e '{{.}} => <template here>'
  4. true => 1
英文:

With Go text/template language, how can I convert a bool to an int (false=0, true=1)?

Here is an example using my own goproc tool that allows to execute template from the command line:

  1. $ echo false | goproc -e &#39;{{.}} =&gt; &lt;template here&gt;&#39;
  2. false =&gt; 0
  3. $ echo true | goproc -e &#39;{{.}} =&gt; &lt;template here&gt;&#39;
  4. true =&gt; 1

答案1

得分: 1

这里有一个技巧:使用index内置函数从一个特制的字符串中提取字节值,该字符串在位置5和4分别包含字节0和1。字符串"true"/"false"的长度被用作索引值。

  1. $ echo false | goproc -e '{{.}} => {{index "....
    $ echo false | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
  2. false => 0
  3. $ echo true | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
  4. true => 1
  5. 1
    $ echo false | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
  6. false => 0
  7. $ echo true | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
  8. true => 1
  9. 0" (len (print .))}}{{"\n"}}'
  10. false => 0
  11. $ echo true | goproc -e '{{.}} => {{index "....
    $ echo false | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
  12. false => 0
  13. $ echo true | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
  14. true => 1
  15. 1
    $ echo false | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
  16. false => 0
  17. $ echo true | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
  18. true => 1
  19. 0" (len (print .))}}{{"\n"}}'
  20. true => 1
英文:

Here is a hack: use the index built-in function to extract byte values from a specially crafted string that contains bytes 0 and 1 respectively at positions 5 and 4. The lengths of strings &quot;true&quot;/&quot;false&quot; are used as the index value.

  1. $ echo false | goproc -e &#39;{{.}} =&gt; {{index &quot;....
    $ echo false | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  2. false =&gt; 0
  3. $ echo true | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  4. true =&gt; 1
  5. 1
    $ echo false | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  6. false =&gt; 0
  7. $ echo true | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  8. true =&gt; 1
  9. 0&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  10. false =&gt; 0
  11. $ echo true | goproc -e &#39;{{.}} =&gt; {{index &quot;....
    $ echo false | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  12. false =&gt; 0
  13. $ echo true | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  14. true =&gt; 1
  15. 1
    $ echo false | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  16. false =&gt; 0
  17. $ echo true | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  18. true =&gt; 1
  19. 0&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
  20. true =&gt; 1

答案2

得分: 1

这可能是最简单的方式:

  1. digitizeBoolTemplate := "{{if . }}1{{else}}0{{end}}"

https://goplay.tools/snippet/T6EwkKLNmhG

英文:

This is probably the simplest:

  1. digitizeBoolTemplate := &quot;{{if . }}1{{else}}0{{end}}&quot;

https://goplay.tools/snippet/T6EwkKLNmhG

huangapple
  • 本文由 发表于 2022年11月19日 00:59:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/74493022.html
匿名

发表评论

匿名网友

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

确定