1
我想做一个API来处理具有路径的请求,如 http:\\localhost:8080\todo\something
但我需要使用自定义服务器。[去]自定义服务器来处理具体路径的请求
这是我写的一段代码。
package main
import (
"net/http"
"fmt"
"io"
"time"
)
func myHandler(w http.ResponseWriter, req *http.Request){
io.WriteString(w, "hello, world!\n")
}
func main() {
//Custom http server
s := &http.Server{
Addr: ":8080",
Handler: http.HandlerFunc(myHandler),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
err := s.ListenAndServe()
if err != nil {
fmt.Printf("Server failed: ", err.Error())
}
}
本post
我的灵感来自处理所有接受该请求这样http:localhost:8080\abc
,http:localhost:8080\abc
等 如何使其只处理请求的路径匹配给定制服务器路径。