Golang html/template.JSStr type examples
package html/template
JSStr encapsulates a sequence of characters meant to be embedded between quotes in a JavaScript expression. The string must match a series of StringCharacters:
StringCharacter :: SourceCharacter but not \
or LineTerminator | EscapeSequence
Note that LineContinuations are not allowed. JSStr("foo\nbar") is fine, but JSStr("foo\\nbar") is not.
Golang html/template.JSStr type usage examples
Example 1:
data := []interface{}{
`<b> "foo%" O'Reilly &bar;`,
CSS(`a[href =~ "//example.com"]#foo`),
HTML(`Hello, <b>World</b> &tc!`),
HTMLAttr(` dir="ltr"`),
JS(`c && alert("Hello, World!");`),
JSStr(`Hello, World & O'Reilly\x21`), // <--- here
URL(`greeting=H%69&addressee=(World)`),
}
Example 2:
...
p.Title = title
slug := filepath.Base(filename)
length := len(slug)
p.Slug = slug[:length-3]
body := MD(buffer.Bytes())
p.Body = template.HTML(string(body))
p.Date = get_time(date)
p.S_Date = date
p.Tags = tags
p.Changed = false
p.Url = fmt.Sprintf("%sposts/%s.html", conf.URL, p.Slug)
p.Durl = template.JSStr(p.Url) // <--- here
...
References :
Advertisement
Something interesting
Tutorials
+9.3k Golang : Timeout example
+17.6k Golang : delete and modify XML file content
+22.4k Golang : How to read JPG(JPEG), GIF and PNG files ?
+17.8k Golang : Defer function inside init()
+10.1k Golang : Print how to use flag for your application example
+6k Golang : Convert Chinese UTF8 characters to Pin Yin
+9.7k Golang : Format strings to SEO friendly URL example
+22.6k Generate checksum for a file in Go
+34k Golang : Proper way to set function argument default value
+11.7k Golang : Gorilla web tool kit secure cookie example
+27.9k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+7.4k Linux : How to fix Brother HL-1110 printing blank page problem