google-app-engine - GO:如何将数据发布到数据存储区?

标签 google-app-engine go

1)通过模板方法渲染了一个登录页面。 例如:这是 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 文件:

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 : ", "\n")
    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,"\n")  
    } 
    fmt.Fprintf(w, "Hello %s!", remPartOfURL)   
   }


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

问题:点击登录按钮后:&g 应该打印用户名和密码值,但显示为空:&g : &{ } 可以做什么?

最佳答案

您可能会尝试将 fmt.Fprintf 语句替换为 http.Error声明。

例如替换:

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

与:

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

您正在直接写入 http.ResponseWriter,这可能会导致问题。

关于google-app-engine - GO:如何将数据发布到数据存储区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17163932/

相关文章:

multithreading - Golang 中的 channel 和 mutex 有什么区别?

python - 使用标准 Google API Python 库时的 httplib.ResponseNotReady

java - 为什么我得到 appcfg.sh : permission denied when i try to download my app from google app engine?

java - 使用谷歌应用引擎将文件上传到 blobstore 时出现 http 400 错误

node.js - Google Cloud Pub/Sub 重试次数

windows - 在 Windows 上使用 exec.Command 进行 noverify

go - errors.Wrapf()、errors.Errorf() 和 fmt.Errorf() 之间有什么区别?

linux - 在 go 中使用 windows dll 库,为 linux 和 mac os x 编译

java - 从 Google App Engine 中的 PHP 文件获取文件

go - 为什么计时器停止会产生死锁错误?