英文:
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 '{{.}} => <template here>'
false => 0
$ echo true | goproc -e '{{.}} => <template here>'
true => 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 "true"
/"false"
are used as the index value.
$ 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
答案2
得分: 1
这可能是最简单的方式:
digitizeBoolTemplate := "{{if . }}1{{else}}0{{end}}"
https://goplay.tools/snippet/T6EwkKLNmhG
英文:
This is probably the simplest:
digitizeBoolTemplate := "{{if . }}1{{else}}0{{end}}"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论