go http server 例子,自定义路由。
package main
import (
"fmt"
"net/http"
"reflect"
"strings"
)
func hello(w http.ResponseWriter, req *http.Request) {
w.Write([]byte("Hello"))
}
type Handlers struct {
}
func (h *Handlers) ResAction(w http.ResponseWriter, req *http.Request) {
fmt.Println("res")
w.Write([]byte("res"))
}
func say(w http.ResponseWriter, req *http.Request) {
pathInfo := strings.Trim(req.URL.Path, "/")
fmt.Println("pathInfo:", pathInfo)
parts := strings.Split(pathInfo, "/")
fmt.Println("parts:", parts)
var action = "ResAction"
fmt.Println(strings.Join(parts, "|"))
if len(parts) > 1 {
fmt.Println("22222222")
action = strings.Title(parts[1]) + "Action"
}
fmt.Println("action:", action)
handle := &Handlers{}
controller := reflect.ValueOf(handle)
method := controller.MethodByName(action)
r := reflect.ValueOf(req)
wr := reflect.ValueOf(w)
method.Call([]reflect.Value{wr, r})
}
func main() {
http.HandleFunc("/hello", hello)
http.Handle("/handle/", http.HandlerFunc(say))
http.ListenAndServe(":8001", nil)
//select {} //阻塞进程
}
访问:http://127.0.0.1:8001/handle/res
package main import ( "io" "net/http" "log" ) // hello world, the web server func HelloServer(w http.ResponseWriter, req *http.Request) { io.WriteString(w, "hello, world!\n") } func main() { http.HandleFunc("/hello", HelloServer) err := http.ListenAndServe(":12345", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
上面是一个简单的go http服务器端程序。
http.ListenAndServe的原型:
func ListenAndServe(addr