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
}
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Elvis
  • 61
  • 4

1 Answers1

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

Elvis
  • 61
  • 4
  • 2
    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?](https://stackoverflow.com/questions/37863374/whats-the-difference-between-responsewriter-write-and-io-writestring/37872799#37872799) – icza May 06 '18 at 07:08