golang_real.jpg
In fact, I hate to move bricks now, but sometimes I still move bricks. If you don't move, you know you have strength.
th-14.jpeg
Client code
Template file, the following is the client code of form submission, which may not be familiar. No more explanation.
<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>
Server code
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"]) } }
- Define the routing control of login, which is a method of login. When the client sends a get request to access the template file read by the / login server and returns it to the client, a login interface is the above template file. After the user completes the user name and password, he submits the odd data to the server in post mode. The client obtains the r.Form and simply prints the form data. This completes a form submission from the client to the server
- Note that to get the form data, the client must first call the r.ParseForm() method
<!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) } }
In the development of web application, we often check the email address, user name, phone number and resident identity. These regular expressions are listed below.
e-mail address
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") }
Phone number
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") }
Matching Chinese
if m, _ := regexp.MatchString("^[\\x{4e00}-\\x{{9fa5}}]+$","Code"); !m { fmt.Println("invalidated chinese") }else{ fmt.Println("validated chinese") }
See go web programming