其實本人現(xiàn)在有點討厭搬磚的工作淘正,但是有的時候還是的搬一搬古徒。不搬那知道自己有力氣拓提。
客戶端代碼
模板文件,下面是表單提交的客戶端代碼可能在熟悉不過了隧膘。就不多解釋了代态。
<form class="login_form" action="/login" method="POST">
<div class="form_input">
<label for="username">username</label>
<input id="username" type="text" name="username">
</div>
<div class="form_input">
<label for="password">password</label>
<input id="password" type="password" name="password" />
</div>
<div class="form_submit">
<input type="submit" value="login">
</div>
</form>
服務(wù)端代碼
func login(w http.ResponseWriter, r *http.Request){
fmt.Println("method: " + r.Method)
r.ParseForm()
if r.Method == "GET"{
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
}else{
fmt.Println("username: ", r.Form["username"])
fmt.Println("password: ", r.Form["password"])
}
}
- 定義 login 的一個方法
login
的路由控制,當客戶端發(fā)起 get 請求來訪問 /login 服務(wù)端讀取模板文件疹吃,返回給客戶端一個登錄界面就是上面的模板文件胆数,用戶完成用戶名和密碼填寫后提交以 post 方式表單數(shù)據(jù)給服務(wù)端時候』ツ梗客戶端獲取r.Form
獲取表單數(shù)據(jù)簡單打印出來必尼。這就完成一次客戶端向服務(wù)端的表單提交 - 注意要獲取表單數(shù)據(jù),客戶端一定要先調(diào)用
r.ParseForm()
方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
background: lightgray;
}
.login_form {
background: lightblue;
padding: 12px;
}
.form_input {
color: white;
height: 24px;
outline: none;
border: none;
}
.form_submit input {
color: dodgerblue;
height: 24px;
font-size: 18px;
background: deepskyblue;
}
</style>
</head>
<body>
<form class="login_form" action="/login" method="POST">
<div class="form_input">
<label for="username">username</label>
<input id="username" type="text" name="username">
</div>
<div class="form_input">
<label for="password">password</label>
<input id="password" type="password" name="password" />
</div>
<div class="form_submit">
<input type="submit" value="login">
</div>
</form>
</body>
</html>
package main
import(
"fmt"
"html/template"
"log"
"net/http"
// "strings"
)
func index(w http.ResponseWriter, r *http.Request){
}
func login(w http.ResponseWriter, r *http.Request){
fmt.Println("method: " + r.Method)
r.ParseForm()
if r.Method == "GET"{
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
}else{
fmt.Println("username: ", r.Form["username"])
fmt.Println("password: ", r.Form["password"])
}
}
func iconHandler(w http.ResponseWriter, r *http.Request) {
}
func main() {
http.HandleFunc("/",index);
http.HandleFunc("/login",login);
server := &http.Server{
Addr:":9090",
}
log.Println("Listening...")
err := server.ListenAndServe()
if err != nil{
log.Fatal("Listen And Server ", err)
}
}
在 web 應(yīng)用開發(fā)中篡撵,我們經(jīng)常會對郵件地址判莉、用戶名、電話號碼以及居民身份中進行校驗育谬,下面列出了這些常用的正則表達式券盅。
電子郵件地址
if m, _ := regexp.MatchString(`([\w.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`,"qq123@qq.com"); !m {
fmt.Println("invalidated email address")
}else{
fmt.Println("validated")
}
手機號碼
if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`,"13840008000"); !m {
fmt.Println("invalidated phonenumber address")
}else{
fmt.Println("validated phonenumber")
}
匹配中文
if m, _ := regexp.MatchString("^[\\x{4e00}-\\x{{9fa5}}]+$","代碼"); !m {
fmt.Println("invalidated chinese")
}else{
fmt.Println("validated chinese")
}
參看 go web 編程