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

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

 更新時(shí)間:2015年03月20日 10:44:28   投稿:hebedich  
本文給大家分享的是一個(gè)使用go語(yǔ)言實(shí)現(xiàn)處理表單輸入的實(shí)例代碼,非常的簡(jiǎn)單,僅僅是實(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ù)組中的所有元素放入一個(gè)字符串。
        // 元素是通過(guò)指定的分隔符進(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è)置訪問(wèn)路由
    http.HandleFunc("/", sayHelloName)
    http.HandleFunc("/login", login)
    //設(shè)置監(jiān)聽(tīng)端口
    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        log.Fatal("ListenAndserve:", err)
    }
}

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

相關(guān)文章

最新評(píng)論

汉阴县| 沁水县| 湘潭县| 孝感市| 吉安县| 南木林县| 泽州县| 府谷县| 惠来县| 韶山市| 阿尔山市| 祁连县| 旬阳县| 启东市| 和政县| 荣成市| 手游| 札达县| 乐东| 光山县| 甘肃省| 四子王旗| 宁津县| 万宁市| 亚东县| 通榆县| 安庆市| 九龙坡区| 陵川县| 鄯善县| 昌乐县| 永兴县| 鱼台县| 奉贤区| 平定县| 樟树市| 漾濞| 收藏| 德钦县| 龙陵县| 齐河县|