近两月以来一直在学习Golang,被其优秀的特性深深吸引,Go语言对Web服务器开发简直不能再方便了,因此在此分享一个简单的Web服务器
package main import ( "net/http" "fmt" ) //服务端编写的业务逻辑处理程序 //w,回复客户端消息 //r,读取客户端发送的消息 func myHandler(w http.ResponseWriter, r *http.Request) { //将网页显示给用户 w.Write([]byte("hello go")) } func main() { //注册处理函数,客户端连接,自动调用指定的处理函数 http.HandleFunc("/", myHandler) //http.HandleFunc("/go", myHandler) //在指定的地址进行监听,开启一个HTTP http.ListenAndServe("127.0.0.1:8001", nil) }