PHP中高级工程师面试重点讲解视频课程
Go快速入门浅显易懂视频教程-基础篇
Go快速入门浅显易懂视频教程-中级篇
go实现表单验证
阅读:1320 分享次数:0

package main

import (
    "fmt"
    "html/template"  
    "log"  
    "net/http"  
    "regexp"  
    "strconv"  
)

func sayHelloName(w http.ResponseWriter, r *http.Request) {  
    fmt.Fprintf(w, "hello box")  
}
  
func login(w http.ResponseWriter, r *http.Request) {  
    if r.Method == "GET" {  
        t, _ := template.ParseFiles("login.html")  
        t.Execute(w, nil)    
    } else if r.Method == "POST" {  
        username := r.FormValue("username")  
        password := r.FormValue("password")  
        phone := r.FormValue("phone")  
        like := r.FormValue("like")  
        sex := r.FormValue("sex")  
        utype := r.FormValue("utype")  

        fmt.Println(like)  
        fmt.Println(sex)  
        fmt.Println(utype)  

        //获取年龄之后转成int型
        age, err := strconv.Atoi(r.FormValue("age"))
        if err != nil {
            w.Write([]byte("数字转化出错了,那么可能就不是数字"))
            return
        }
        if username == "" || password == "" || age == 0 {
            w.Write([]byte("username and password and age must not null"))
            return
        }
        //获取数据判定大小
        if age > 100 {
            w.Write([]byte("age is to big"))
            return
        }
        if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`, phone); !m {
            w.Write([]byte("phone is error"))
            return
        }

    } else {
        fmt.Println("error")
    }
}

func main() {  
    http.HandleFunc("/", sayHelloName)  
    http.HandleFunc("/login", login)  
    err := http.ListenAndServe(":8081", nil)  
    if err != nil {  
        log.Fatal("ListenAndServe:", err)  
    }  
}  

感觉本站内容不错,读后有收获?