go form

2018-01-04  本文已影响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

上一篇 下一篇

猜你喜欢

热点阅读