Sun, 22 Mar 2015 12:17 am
Ane awalnya rumit sekali mikirnya ... syukurlah ketemu link ini 
http://stackoverflow.com/questions/17306358/golang-removing-fields-from-struct-or-hiding-them-in-json-response

Ternyata cukup serahkan pada map[string]interface{}, sudah bisa kirim variable apapun, sepuasnya ke templates

  func WkTagViewHandler(w http.ResponseWriter, r *http.Request) {
          
          tagName := mux.Vars(r)["tag"]
          
          funcMap := template.FuncMap{
              "safehtml": func(text string) template.HTML { 

                  text = text + " "
                  text = strings.Replace(text, "<", "<", -1)   
                  text = strings.Replace(text, ">", ">", -1) 
                    
                  text = strings.Replace(text, "
", "
", -1) 
                  text = strings.Replace(text, "
", "
", -1) regexPagar, _ := regexp.Compile(tagPattern) tags := regexPagar.FindAllString(text, -1) for _ , value := range tags { tagAscii := strings.Replace(value, "#", "#", 1) tagOnly := strings.Replace(value, "#", "", 1) text = strings.Replace(text, value, "" + tagAscii + "", 1) } return template.HTML(text) }, } templates := template.Must(template.New("").Funcs(funcMap).ParseFiles(templatePath + "webkoe-base.html", templatePath + "webkoe-tagview.html")) var results []Blog err = MCol.Find(bson.M{"tag": tagName}).Sort("-date").Limit(1000000000000).All(&results) if err != nil { panic(err) } finalResult := map[string]interface{}{ "tagName" : tagName, "tagList" : results, } err := templates.ExecuteTemplate(w, "base", finalResult) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } }
#webkoe #go #script
185
Additional Info: