有无巨佬给个解析,只能看懂表单提交数据
// http-getpost
// 解析了一个身份证号码,判断是否是成年人
// by FR!3Nd(wym)
package main
import (
"io"
"log"
"net/http"
"strconv"
"time"
"unicode"
)
func Substr(str string, start int, end int) string {
rs := []rune(str)
length := len(rs)
if start < 0 || start > length {
panic("start is wrong")
}
if end < 0 || end > length {
panic("end is wrong")
}
return string(rs[start:end])//数组切片,是个会golang的人都能看懂 -- FR!3Nd
}
func inputHandle(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, `
<head>
<meta charset="UTF-8">
</head>
<body>
<form method="POST" action="result">
请输入你的身份证号码 <input type="password" name="sfz" class="sfz" />
<input type="submit" value="填好了,提交!" />
</form>
</body>
<footer>
<p style="color: red">Create by FR!3Nd</p>
</footer>
`)
}
func DispatchHandle(w http.ResponseWriter, r *http.Request) {
s := r.PostFormValue("sfz")
if len(s) != 18 {
io.WriteString(w, `<p style="color: red">输入号码有误!</p>`)
return
}
for _, k := range s {
if !unicode.IsDigit(k) && k != rune('X') {
io.WriteString(w, `<p style="color: red">输入号码有误!</p>`)
return
}
}
yn := time.Now().Year()
ys, _ := strconv.Atoi(Substr(s, 5, 8))
if yn-ys >= 18 {
io.WriteString(w, `<p style="color:green">您是成年人。</p>`)
} else {
io.WriteString(w, `<p style="color:red">您不是成年人</p>`)
}
}
func main() {
http.HandleFunc("/", inputHandle)
http.HandleFunc("/result", DispatchHandle)
err := http.ListenAndServe(":8080", nil)
if err != nil {
panic(err.Error())
}
}