最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

go語言實(shí)現(xiàn)處理表單輸入

 更新時間:2015年03月20日 10:44:28   投稿:hebedich  
本文給大家分享的是一個使用go語言實(shí)現(xiàn)處理表單輸入的實(shí)例代碼,非常的簡單,僅僅是實(shí)現(xiàn)了用戶名密碼的驗(yàn)證,有需要的小伙伴可以自由擴(kuò)展下。

login.html

復(fù)制代碼 代碼如下:

<html>
<head><title></title></head>
<body>
    <form action="http://localhost:9090/login" method="post">
        用戶名:<input type="text" name="username">
        密  碼:<input type="text" name="password">
        <input type="submit" value="登錄">
    </form>
</body>
</html>

main.go

復(fù)制代碼 代碼如下:

package main
import (
    "fmt"
    "html/template"
    "log"
    "net/http"
    "strings"
)
func sayHelloName(w http.ResponseWriter, r *http.Request) {
    // 解析url傳遞的參數(shù)
    r.ParseForm()
    //在服務(wù)端打印信息
    fmt.Println(r.Form)
    fmt.Println("path", r.URL.Path)
    fmt.Println("Scheme", r.URL.Scheme)
    fmt.Println(r.Form["url_long"])
    for k, v := range r.Form {
        fmt.Println("key:", k)
        // join() 方法用于把數(shù)組中的所有元素放入一個字符串。
        // 元素是通過指定的分隔符進(jìn)行分隔的
        fmt.Println("val:", strings.Join(v, ""))
    }
    // 輸出到客戶端
    fmt.Fprintf(w, "hello astaxie!")
}
func login(w http.ResponseWriter, r *http.Request) {
    fmt.Println("method:", r.Method)
    if r.Method == "GET" {
        t, _ := template.ParseFiles("login.html")
        // 執(zhí)行解析模板
        // func (t *Template) Execute(wr io.Writer, data interface{}) error {
        t.Execute(w, nil)
    } else {
        r.ParseForm()
        fmt.Println("username:", r.Form["username"])
        fmt.Println("password:", r.Form["password"])
    }
}
func main() {
    //設(shè)置訪問路由
    http.HandleFunc("/", sayHelloName)
    http.HandleFunc("/login", login)
    //設(shè)置監(jiān)聽端口
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        log.Fatal("ListenAndserve:", err)
    }
}

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

相關(guān)文章

最新評論

睢宁县| 津市市| 新田县| 城市| 昌黎县| 资兴市| 出国| 屏东县| 德州市| 德格县| 汾西县| 韶关市| 天柱县| 东光县| 鲁山县| 湖北省| 马尔康县| 北宁市| 全南县| 巴南区| 芮城县| 闵行区| 浦东新区| 蒙山县| 个旧市| 历史| 伊宁市| 武隆县| 闵行区| 泰来县| 五台县| 九寨沟县| 宁阳县| 临猗县| 洱源县| 双峰县| 丰镇市| 邵东县| 健康| 曲阳县| 清水河县|