**方式1**
```go
package main
import (
"fmt"
"net/http"
)
func main(){
//处理路由为 / 的方法
http.HandleFunc("/",func(w http.ResponseWriter,r *http.Request){
fmt.Fprintln(w,"Hello World",r.URL.Path)
})
//监听3000端口
http.ListenAndServe(":3000",nil)
}
```
**方式2**
```go
package main
import (
"fmt"
"net/http"
)
type MyHandler struct{
}
//实现接口的ServeHTTP方法
func (this *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request){
fmt.Fprintln(w,"通过实现接口Handler的ServeHTTP方法来创建服务器")
}
func main(){
myHandler := &MyHandler{}
http.Handle("/",myHandler)
http.ListenAndServe(":8888",nil)
}
```
有疑问加站长微信联系(非本文作者))