login.html
復(fù)制代碼 代碼如下:
html>
head>title>/title>/head>
body>
form action="http://localhost:9090/login" method="post">
用戶名:input type="text" name="username">
密 碼:input type="text" name="password">
input type="submit" value="登錄">
/form>
/body>
/html>
main.go
復(fù)制代碼 代碼如下:
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"strings"
)
func sayHelloName(w http.ResponseWriter, r *http.Request) {
// 解析url傳遞的參數(shù)
r.ParseForm()
//在服務(wù)端打印信息
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)
// join() 方法用于把數(shù)組中的所有元素放入一個字符串。
// 元素是通過指定的分隔符進行分隔的
fmt.Println("val:", strings.Join(v, ""))
}
// 輸出到客戶端
fmt.Fprintf(w, "hello astaxie!")
}
func login(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
t, _ := template.ParseFiles("login.html")
// 執(zhí)行解析模板
// func (t *Template) Execute(wr io.Writer, data interface{}) error {
t.Execute(w, nil)
} else {
r.ParseForm()
fmt.Println("username:", r.Form["username"])
fmt.Println("password:", r.Form["password"])
}
}
func main() {
//設(shè)置訪問路由
http.HandleFunc("/", sayHelloName)
http.HandleFunc("/login", login)
//設(shè)置監(jiān)聽端口
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndserve:", err)
}
}
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:- django之從html頁面表單獲取輸入的數(shù)據(jù)實例
- Python中使用django form表單驗證的方法
- django1.8使用表單上傳文件的實現(xiàn)方法
- Python的Django框架中forms表單類的使用方法詳解
- go web 處理表單的輸入的說明