golang web 從網頁地址欄得到參數,而後輸出

地址欄輸入
http://localhost:8880/user?Id=1&name=guo
git


package main
import (
    //"database/sql"
    //_ "github.com/mattn/go-sqlite3"
    "fmt"
    "log"
    "net/http"
    //"strconv"
)

func addUser(w http.ResponseWriter, req *http.Request) {
    userId := req.FormValue("Id")
    name := req.FormValue("name")
    out := userId + "-" + name
        log.Println(out)
    fmt.Fprintf(w, out)
}

func sayHello(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "hello world!")
}

func main() {
    http.HandleFunc("/", sayHello)
    http.HandleFunc("/user", addUser)
    err := http.ListenAndServe(":8880", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err.Error())
    }
}
github

相關文章
相關標籤/搜索