从上面访问的结果发现。我们这边的< 之类的符号已经被转义成html 特殊符
上面的方法也是可以进行简化的
package main import ( "fmt" "html/template" "log" "net/http" ) func main() { //绑定路由 讲访问 / 绑定给 Handler 方法进行处理 http.HandleFunc("/", Handler) http.ListenAndServe(":8080", nil) } func Handler(w http.ResponseWriter, req *http.Request) { err := req.ParseForm() //如果解析失败 直接退出 输出对应的错误原因 if err != nil { log.Fatal(nil) } //获取 传递的name 参数 user_pro := req.FormValue("name") fmt.Fprintf(w, "%s", xss_hander(user_pro)) } func xss_hander(s string) string { return template.HTMLEscapeString(s) }
https://blog.csdn.net/liangguangchuan/article/details/54617685