7Days/day1/base3/main.go

32 lines
708 B
Go
Raw Permalink Normal View History

2023-01-24 21:14:53 +08:00
package main
import (
"fmt"
"gee"
"net/http"
)
func main() {
2023-02-04 19:00:36 +08:00
// 创建 Engine 实例
2023-01-24 21:14:53 +08:00
r := gee.New()
2023-02-04 19:00:36 +08:00
// 增加一个 GET 路由, path 为 /
2023-01-24 21:14:53 +08:00
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Hello World"))
})
2023-02-04 19:00:36 +08:00
// 增加一个 GET 路由, path 为 /hello
2023-01-24 21:14:53 +08:00
r.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
for k, v := range r.Header {
_, _ = fmt.Fprintf(w, "Header[%q] = %q", k, v)
}
})
2023-02-04 19:00:36 +08:00
// 增加一个 POST 路由, path 为 /login
2023-01-24 21:14:53 +08:00
r.POST("/login", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, "POST login")
})
2023-02-04 19:00:36 +08:00
// 创建服务端口的监听并开启 Http 服务
err := r.Run(":8000")
2023-01-24 21:14:53 +08:00
if err != nil {
return
}
}