Avoid escape characters and print raw data #1374
-
Hi @hairyhenderson , Use case is as follows, When I am trying to print the string, it is escaping the We wanted to print the raw value of the key, which is Is there any option in gomplate which we can use to print a string with raw value? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @veera-chenna, I've converted this to a discussion as this doesn't seem to be a bug or feature request.
To the contrary, the JSON string contains a I think what you want to do is quote the string so that it's re-escaped. There are a few ways you could approach this, but one way is to use For example: $ gomplate -i '{{ $s := "no\\escape" }}{{ printf "%q" $s }}'
"no\\escape" You could also use the $ gomplate -i '{{ $s := "no\\escape" }}{{ strings.Quote $s }}'
"no\\escape" Also equivalent in this case would be $ gomplate -i '{{ $s := "no\\escape" }}{{ data.ToJSON $s }}'
"no\\escape" However all of these methods are escaping the string for the purpose of quoting it, so you'll always end up with the string surrounded by double-quotes. If you want the literal raw string Hope this helps! |
Beta Was this translation helpful? Give feedback.
Hi @veera-chenna, I've converted this to a discussion as this doesn't seem to be a bug or feature request.
To the contrary, the JSON string contains a
\
which is escaped as\\
, so gomplate is behaving correctly by un-escaping it as you output the value.I think what you want to do is quote the string so that it's re-escaped. There are a few ways you could approach this, but one way is to use
printf
with the%q
("quote") formatter:For example:
You could also use the
strings.Quote
function, which is equivalent:$ gomplate -i '{{ $s := …