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

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

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)执行命令行模板的示例:

$ echo false | goproc -e '{{.}} => <template here>'
false => 0
$ echo true | goproc -e '{{.}} => <template here>'
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:

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

答案1

得分: 1

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

$ echo false | goproc -e '{{.}} => {{index "....
$ echo false | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
false => 0
$ echo true | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
true => 1
1
$ echo false | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
false => 0
$ echo true | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
true => 1
0" (len (print .))}}{{"\n"}}'
false => 0 $ echo true | goproc -e '{{.}} => {{index "....
$ echo false | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
false => 0
$ echo true | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
true => 1
1
$ echo false | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
false => 0
$ echo true | goproc -e '{{.}} => {{index "....\001\000" (len (print .))}}{{"\n"}}'
true => 1
0" (len (print .))}}{{"\n"}}'
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.

$ 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;
false =&gt; 0
$ echo true | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
true =&gt; 1
1
$ echo false | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
false =&gt; 0
$ echo true | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
true =&gt; 1
0&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
false =&gt; 0 $ 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;
false =&gt; 0
$ echo true | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
true =&gt; 1
1
$ echo false | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
false =&gt; 0
$ echo true | goproc -e &#39;{{.}} =&gt; {{index &quot;....\001\000&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
true =&gt; 1
0&quot; (len (print .))}}{{&quot;\n&quot;}}&#39;
true =&gt; 1

答案2

得分: 1

这可能是最简单的方式:

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

https://goplay.tools/snippet/T6EwkKLNmhG

英文:

This is probably the simplest:

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:

确定