處理表單的輸入
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
使用者名稱:<input type="text" name="username">
密碼:<input type="password" name="password">
<input type="submit" value="登入">
</form>
</body>
</html>package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
func sayhelloName(w http.ResponseWriter, r *http.Request) {
r.ParseForm() //解析 url 傳遞的參數,對於 POST 則解析 HTTP 回應內容的主體(request body)
//注意 : 如果沒有呼叫 ParseForm 方法,下面無法取得表單的資料
fmt.Println(r.Form) //這些資訊是輸出到伺服器端的列印資訊
fmt.Println("path", r.URL.Path)
fmt.Println("scheme", r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println("key:", k)
fmt.Println("val:", strings.Join(v, ""))
}
fmt.Fprintf(w, "Hello astaxie!") //這個寫入到 w 的是輸出到客戶端的
}
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method) //取得請求的方法
if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
log.Println(t.Execute(w, nil))
} else {
//請求的是登入資料,那麼執行登入的邏輯判斷
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
}
}
func main() {
http.HandleFunc("/", sayhelloName) //設定存取的路由
http.HandleFunc("/login", login) //設定存取的路由
err := http.ListenAndServe(":9090", nil) //設定監聽的埠
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}

links
Last updated