Golang html/template.URL type examples

package html/template

URL encapsulates a known safe URL or URL substring (see RFC 3986). A URL like javascript:checkThatFormNotEditedBeforeLeavingPage() from a trusted source should go in the page, but by default dynamic javascript: URLs are filtered out since they are a frequently exploited injection vector.

Golang html/template.URL type usage examples

Example 1:

 func (_ codeGooglePresenter) Image() template.URL {
  return "https://github.com/images/gravatars/gravatar-user-420.png"
 }

Example 2:

 package main

 import (
 "fmt"
 "html/template"
 "net/url"
 )

 func main() {

 search := url.Values{
 "q": {"wikipedia"},
 }

 url := url.URL{
 Scheme: "https",
 Host: "google.com",
 Path: "/",
 RawQuery: search.Encode(),
 }

 s := url.String()

 tURL := template.URL(s)

 fmt.Println(tURL)
 }

Output :

https://google.com/?q=wikipedia

References :

https://github.com/shurcooL/Go-Package-Store/blob/master/presenter/codegoogle.go

http://golang.org/pkg/html/template/#URL

Advertisement