GO:如何将数据发布到数据存储中?

1)Rendered a Login Page through template method. Ex.: this is index.html

{{ define "title" }}Guestbook{{ end }}

    {{ define "content" }}
        <form action="/login" method="post">
          <div><label>UserName : </label><input name="username" type="text" /></div>
          <div><label>Password : </label><input name="password" type="password" /></div>
          <div><input type="submit" value="login"></div>
        </form>

    {{ end }}

2) hello.go file:

package main

import (
  "fmt"
  "html/template"
  "net/http"
)

var index = template.Must(template.ParseFiles(
  "templates/base.html",
  "templates/index.html",
))
type UserLogin struct{
    UserName string
    PassWord string
}


func handler(w http.ResponseWriter, r *http.Request) {  
    index.Execute(w, nil)    
}



/*func login(w http.ResponseWriter, r *http.Request) {  
        remPartOfURL := r.URL.Path[len("/login/"):] 
        if r.Method == "POST" {
           fmt.Fprintf(w, "after",r.FormValue("username"))       
        }   
        fmt.Fprintf(w, "Hello %s!", remPartOfURL)
    }*/
    func login(w http.ResponseWriter, r *http.Request) {    
        fmt.Fprint(w, "login : ", "
")
    remPartOfURL := r.URL.Path[len("/login/"):]     
    if r.Method == "POST" {         
        g := UserLogin{
            UserName: r.FormValue("username"),
            PassWord: r.FormValue("password"),
        }
        fmt.Fprint(w, "&g : ", &g,"
")  
    } 
    fmt.Fprintf(w, "Hello %s!", remPartOfURL)   
   }


func init() {
    http.HandleFunc("/", handler)
    http.HandleFunc("/login/", login)
}

Question: After clicked on Login button: &g should print username and password values but it is showing empty: &g : &{ } What can be done?

One thing you might try is replacing the fmt.Fprintf statements with http.Error statements.

For example replace:

fmt.Fprintf(w, "after",r.FormValue("username"))

with:

http.Error(w, fmt.Sprintf("after %s", r.FormValue("username")), http.StatusOK)

You're writing directly to the http.ResponseWriter which can cause problems.