Skip to content

Instantly share code, notes, and snippets.

@dolmen
Last active December 1, 2022 09:00
Show Gist options
  • Save dolmen/a3b8d7945e466cf1106910b25b1c9237 to your computer and use it in GitHub Desktop.
Save dolmen/a3b8d7945e466cf1106910b25b1c9237 to your computer and use it in GitHub Desktop.
Go text/template hacks

Go text/template hacks

Hacks below use goproc to experiment with Go templates from the command-line.

Convert bool to int

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

Display true in red, false in green

$ echo false | goproc -e '{{printf "\033[1;3%cm%v\033[m\n" (index "....12" (len (print .))) .}}'
$ echo true | goproc -e '{{printf "\033[1;3%cm%v\033[m\n" (index "....12" (len (print .))) .}}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment