0

Im trying to build a small website, I use the html/template to create dynamic pages. One thing on the pages is a list of URL's inside those urls sometimes I need character encoding. for special characters like ô (%C3%B4).

When i try to parse the variables into a page using html/template i get the following as a result: %!c(MISSING)3%!b(MISSING)4. I have no clue what is wrong here

type Search_list struct { Search_name string Search_url string Search_price float64 } func generateSearchPage(language int, q string) (string, error) { /* ommited, fetshing data from elasticsrearch*/ sl := []Search_list{} var urle *url.URL //looping through ES results and putting them in a custom List for _, res := range data.Hits.Hits { // //Encode Url var err error urle, err = url.Parse(res.Source.URL) if err != nil { continue // TODO: add log } //I've tried already the following: fmt.Println(res.Source.URL) //ô fmt.Println(url.QueryUnescape(res.Source.URL)) //ô fmt.Println(urle.String()) //%C3%B4 u, _ := url.QueryUnescape(res.Source.URL) sl = append(sl, Search_list{res.Source.Name, u, res.Source.Price}) } var buffer bytes.Buffer t := template.New("Index template") t, err = t.Parse(page_layout[language][PageTypeSearch]) if err != nil { panic(err) } err = t.Execute(&buffer, Search_data{ Title: translations[language]["homepage"], Page_title: WebSiteName, Listed_items: sl, }) if err != nil { panic(err) } return buffer.String(), nil // %!c(MISSING)3%!b(MISSING)4 } 
1
  • Can you provide the HTML template? Commented May 5, 2018 at 18:52

1 Answer 1

1

@ Moshe Revah thanks for the help, in the meantime I found the error

Later in the code I send my generated page to the http client with

fmt.Fprintf(w, page) // Here was the error b/c of the % symbols 

I just changed it to

fmt.Fprint(w, page) 

and it works perfect

Sign up to request clarification or add additional context in comments.

1 Comment

Note that you can just as easily use w.Write(page). For differences and more options, see What's the difference between ResponseWriter.Write and io.WriteString?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.