[go-nuts] Re: Html is not parsing while parsing a template to send email

2018-05-04 Thread djego . joss
Hello,

first a small remark on wording: I think you're using the verb "parse" with 
the wrong definition here? You might mean something like "to replace html 
in template tokens" instead of "to parse..." :-).

On Friday, May 4, 2018 at 10:42:57 AM UTC+2, Amandeep Kaur wrote:
>
> Where templateHtml is the email body with tokens and data is the interface 
> holding dynamic values for these tokens. When I use ParseTemplate function 
> to parse tokens as string values then it works fine. But if I have to parse 
> html in one of my tokens then it parses html as string and in email 
> displays html as string.
>

This is actually a "feature" caused by the html/template package security 
model. You can read more on this in the 
documentation: https://golang.org/pkg/html/template/#hdr-Security_Model
What happens is that content of `data interface{}`, typically provided by 
the user and not the programmer, is considered "unsafe". Thus html in there 
will be escaped.
However, if you (the programmer) know that the content of the specific 
token `{{.TOKENNAME}}` is "safe" html, then you can cast it to 
html/template.HTML (see https://golang.org/pkg/html/template/#HTML) before 
Executing the template.
 

> Can anybody tell me what should I do to parse html in ParseTemplate 
> function??
>
>  Hope this clears up your problem,
Diego 

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Re: Go string and UTF-8

2019-08-20 Thread djego . joss
On Tue, Aug 20, 2019 at 10:12 AM Pierre Durand wrote:
>
> I know that by convention Go string contain UTF-8 encoded text.

To my understanding this is not entirely true -- see 
https://blog.golang.org/strings#TOC_2. -- It is simply a readonly slice of 
bytes. However there is at least 2 places where UTF-8 encoding is used for 
strings in the language spec: source code file is expected to be UTF-8 
(thus string literals are partially influenced), and when using the `for 
range` construct on a string. Otherwise there are various packages (e.g. 
unicode/utf8) which expect UTF-8 encoded strings as arguments.

> Is it recommended/a good practice to store invalid bytes in a string ?

Thus the concept of _invalid bytes in a string_ doesn't really exist ;-).

> The use case:
> - compute a hash => get a []byte
> - convert the []byte to string (this string is not UTF-8 valid)
> - use the string as a map key

I don't see any issues with this.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/34d7cce7-91d0-454f-ab1c-c373a984d66f%40googlegroups.com.