golang 处理http请求
端口9089
package main
import (
"fmt"
"net/http"
)
func main() {
fmt.Println("服务启动成功!")
http.HandleFunc("/test", myHandler)
http.ListenAndServe(":9089", nil)
}
func myHandler(resp http.ResponseWriter, req *http.Request) {
fmt.Println("收到请求!")
resp.Write([]byte("Hello world!"))
}
运行
访问http://127.0.0.1:9089/test
返回Hello world!