Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Golang入门指南 - 压测和调优 #23

Open
xpzouying opened this issue Nov 17, 2020 · 0 comments
Open

Golang入门指南 - 压测和调优 #23

xpzouying opened this issue Nov 17, 2020 · 0 comments
Labels

Comments

@xpzouying
Copy link
Owner

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))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant