![e0824bff7dae6a90ba84b71234eb1d87.png](https://i-blog.csdnimg.cn/blog_migrate/9d9115b17e59e5a64f7a158acb64f7b7.jpeg)
上一篇讲解了client端相关的知识,这一篇讲解服务端。golang语言可以快速实现一个简单的server端,如下所示:
package main
import (
"net/http"
"log"
)
type TestHandler struct {
str string
}
func SayHello(w http.ResponseWriter, r *http.Request){
log.Printf("HandleFunc")
w.Write([]byte(string("HandleFunc")))
}
//ServeHTTP方法,绑定TestHandler
func (th *TestHandler)ServeHTTP(w http.ResponseWriter, r *http.Request){
log.Printf("Handle")
w.Write([]byte(string("Handle")))
}
func main(){
http.Handle("/", &TestHandler{
"Hi"})//根路由
http.HandleFunc("/test", SayHello) //test路由
http.ListenAndServe("127.0.0.1:8000