golang萌新求助
  • 板块灌水区
  • 楼主Caviar_X
  • 当前回复0
  • 已保存回复0
  • 发布时间2021/5/28 21:18
  • 上次更新2023/11/4 22:35:58
查看原帖
golang萌新求助
278124
Caviar_X楼主2021/5/28 21:18

有无巨佬给个解析,只能看懂表单提交数据

// 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())
	}
}

2021/5/28 21:18
加载中...