码迷,mamicode.com
首页 > 其他好文 > 详细

go 表单

时间:2019-05-20 18:58:42      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:action   method   html   http   string   writer   handle   ase   htm   


package main
import (
    "fmt"
    "io"
    "net/http"
)

const form = `<html><body><form action="#" method="post" name="bar">
                    <input type="text" name="in"/>
                    <input type="text" name="in"/>
                     <input type="submit" value="Submit"/>
             </form></html></body>`

func SimpleServer(w http.ResponseWriter, request *http.Request) {
    n, err := io.WriteString(w, "<h1>hello, world</h1>")
    if err != nil{
        fmt.Println(n)
    }
}

func FormServer(w http.ResponseWriter, request *http.Request) {
    w.Header().Set("Content-Type", "text/html")
    switch request.Method {
    case "GET":
        io.WriteString(w, form)
    case "POST":
        request.ParseForm()
        io.WriteString(w, request.Form["in"][0])
        io.WriteString(w, "\nss")
        io.WriteString(w, request.FormValue("in"))
    }
}

func Test(w http.ResponseWriter, r *http.Request){
    fmt.Println("handler hello")
    n, err := fmt.Fprintf(w, "hello world!")
    fmt.Println(n)
    if err != nil{
        fmt.Println("write error:", n)
    }
}

func main() {
    http.HandleFunc("/", Test)
    http.HandleFunc("/test1", SimpleServer)
    http.HandleFunc("/test2", FormServer)
    if err := http.ListenAndServe("127.0.0.1:80", nil); err != nil {
        fmt.Println("http listen eror")
    }
}

go 表单

标签:action   method   html   http   string   writer   handle   ase   htm   

原文地址:https://www.cnblogs.com/lajiao/p/10895731.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!