go form

作者: 灵山路远 | 来源:发表于2018-01-04 09:51 被阅读0次

1 main.go

package main

import (

"fmt"

"html/template"

"log"

"net/http"

"strings"

)

func sayhelloName(w http.ResponseWriter, r *http.Request) {

r.ParseForm()

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)

fmt.Println("val: ", strings.Join(v, ""))

}

fmt.Fprintf(w, "Hello astaxie!")

}

func login(w http.ResponseWriter, r *http.Request) {

r.ParseForm()

fmt.Println("method:", r.Method)

if r.Method == "GET" {

t, _ := template.ParseFiles("login.html")

t.Execute(w, nil)

} else {

fmt.Println(r.Form)

fmt.Println("path", r.URL.Path)

fmt.Println("scheme", r.URL.Scheme)

fmt.Println("username:", r.Form["username"])

fmt.Println("password:", r.Form["password"])

}

}

func main() {

http.HandleFunc("/", sayhelloName)

http.HandleFunc("/login", login)

err := http.ListenAndServe(":9090", nil)

if err != nil {

log.Fatal("ListenAndServe: ", err)

}

}

2 login.html

相关文章