簡単なフォーム処理のhttpを行きます

//フォーム処理
パッケージメイン

インポート(
    "ネット/ HTTP"
    "IO"
    "FMT"
    "ログ"

constのフォーム= `<HTML> <BODY>
        <フォームアクション= "#" メソッド= "ポスト" />
            <input type = "text" NAME = "名前" />
            <input type = "text" NAME = "パス" />
            <input type = "提出" 値= "提出" />
        </フォーム>
        </ body> </ html>この `

FUNCのdeal2(W http.ResponseWriter、R * http.Request){
    w.Header()。セット( "Content-Typeの"、 "text / htmlの")

    指定されたメソッドのHTTP //メソッド代わっ
    r.Methodを{スイッチ
        場合は、 "GET":
            //io.WriteString(w、S列)ワットに文字列sを書き込み
            io.WriteString(W、フォーム)
        ケース "POST":
            // FUNC(*リクエスト)PostFormValue
            // PostFormValueは、文字列の最初のスライスの結合r.PostFormクエリ結果[]の値を取得するためにキーフィールドを返します
            (r.PostFormValue( "名前")、ワット)io.WriteString
            io.WriteString(ワット、 "\ n" は)
            (r.PostFormValue( "パス")、W)io.WriteString
    }
}       

//パニック治療
FUNC logsPanic(http.HandlerFuncハンドル)http.HandlerFunc {
    戻りFUNC(W http.ResponseWriter、R * http.Request){
        {)(FUNCを延期
            X場合:=回復()。X!= nilの{
                log.Printf( "[%V]キャッチパニック:%のV"、r.RemoteAddr、X)
            }
        }()
        ハンドル(W、R)
    }
}


FUNCのメイン(){
    http.ResponseWriter、R * http.Request W http.HandleFunc( "/ TEST1"、FUNC(){
        fmt.Fprintf(ワット、 "<H1>のHello World </ H1>")
    })

    http.HandleFunc( "/ TEST2"、logsPanic(deal2))

    ERRの場合:= http.ListenAndServe( "localhostを:10010"、nilを); ERR!= nilの{
        fmt.Println( "サーバー失敗し、エラーを聞く:"、ERR)
        リターン
    }

}

  

おすすめ

転載: www.cnblogs.com/zhangxiaoj/p/11297002.html