Open
Description
main.go代码如下,演示如何对下列代码进行测试、压测及调优。
package main
import (
"fmt"
"log"
"net/http"
"regexp"
)
var counter = map[string]int{}
func handleHello(w http.ResponseWriter, r *http.Request) {
color := r.FormValue("color")
if match, _ := regexp.MatchString(`^[a-zA-Z]+$`, color); !match {
http.Error(w, "Optional color is invalid", http.StatusBadRequest)
return
}
name := r.FormValue("name")
counter[name]++
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte("<h1 style='color: " + color +
"'>Welcome!</h1> <p>Name: " + name + "</p> <p>Count: " + fmt.Sprint(counter[name]) + "</p>"))
}
func main() {
http.HandleFunc("/hello", handleHello)
log.Fatal(http.ListenAndServe(":8080", nil))
}