未收到go语言表单的任何数据

未收到go语言表单的任何数据,go,Go,这是astaxie书中的简单形式 当我尝试“/login”时,我得到 No Data received { Unable to load the webpage because the server sent no data. Error code: ERR_EMPTY_RESPONSE } 代码如下: main.go package main import ( "fmt" "html/template" "net/http" ) func sayhelloNa

这是astaxie书中的简单形式 当我尝试“/login”时,我得到

No Data received { Unable to load the webpage because the server sent no data.   Error code: ERR_EMPTY_RESPONSE }
代码如下:

main.go

package main
import (
    "fmt"
    "html/template"
    "net/http"

)

func sayhelloName(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello astaxie!") // write data to response
}

func login(w http.ResponseWriter, r *http.Request) {
    fmt.Println("method:", r.Method) //get request method
    if r.Method == "GET" {
      t, err :=template.New("").Parse(loginHtml)
      if err != nil {
          panic(err)
      }

      const loginHtml = `
      <html>
      <head>
      <title></title>
      </head>
      <body>
      <form action="/login" method="post">
          Username:<input type="text" name="username">
          Password:<input type="password" name="password">
          <input type="submit" value="Login">
      </form>
      </body>
      </html>
      `

    }    else {
        r.ParseForm()
        // logic part of log in
        fmt.Println("username:", r.PostFormValue("username"))
        fmt.Println("password:", r.PostFormValue("password"))
    }
}


func main() {
    http.HandleFunc("/", sayhelloName) // setting router rule
    http.HandleFunc("/login", login)
   http.ListenAndServe(":9090", nil) // setting listening port

}

#login.gtpl
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
    Username:<input type="text" name="username">
    Password:<input type="password" name="password">
    <input type="submit" value="Login">
</form>
</body>
</html>

Any idea??
主程序包
进口(
“fmt”
“html/模板”
“net/http”
)
func sayhelloName(w http.ResponseWriter,r*http.Request){
fmt.Fprintf(w,“Hello astaxie!”)//将数据写入响应
}
func登录(w http.ResponseWriter,r*http.Request){
Println(“方法:,r.method)//获取请求方法
如果r.Method==“GET”{
t、 err:=template.New(“”).Parse(loginHtml)
如果错误!=零{
恐慌(错误)
}
const loginHtml=`
用户名:
密码:
`
}否则{
r、 ParseForm()
//登录的逻辑部分
fmt.Println(“用户名:”,r.PostFormValue(“用户名”))
fmt.Println(“密码:”,r.PostFormValue(“密码”))
}
}
func main(){
http.HandleFunc(“/”,sayhelloName)//设置路由器规则
http.HandleFunc(“/login”,login)
http.ListenAndServe(“:9090”,nil)//设置侦听端口
}
#login.gtpl
用户名:
密码:
有什么想法吗??

原始问题(已编辑多次)的问题是
ParseFiles()
函数失败,无法读取模板文件。您不知道这一点,因为它返回给您的
错误刚刚被丢弃。永远不要那样做!您至少可以打印错误,或者在错误发生时调用
panic(err)
。如果你那样做了,你会立刻看到原因

如果指定相对路径,则必须将
login.gtpl
文件放置在启动应用程序的工作目录中。或者指定一个绝对路径

您也可以像这样将HTML源代码放入Go文件中,直到您将问题解决:

t, err := template.New("").Parse(loginHtml)
if err != nil {
    panic(err)
}
t.Execute(w, nil)
// ... the rest of your code


// At the end of your .go source file (outside of login() func) insert:
const loginHtml = `
<html>
<head>
<title></title>
</head>
<body>
<form action="/login" method="post">
    Username:<input type="text" name="username">
    Password:<input type="password" name="password">
    <input type="submit" value="Login">
</form>
</body>
</html>
`
注意#2:

Request.Form
仅在调用了
Request.ParseForm()
之后才可用,因此在访问它之前请执行此操作。同样,对于
POST
表单,您可能希望改用
Request.PostForm

另一种方法是,如果尚未调用,您可以使用自动执行此操作的方法:

fmt.Println("username:", r.PostFormValue("username"))
fmt.Println("password:", r.PostFormValue("password"))

它与main.go{GOPATH/src/}@RamanSM位于同一位置。如果指定相对路径,它必须位于启动应用程序的当前文件夹中。请参阅我编辑的答案。@RamanSM也请在此处删除您的评论,我也会这样做。为了让问题和答案保持干净。我要吃点东西,稍后会检查(如果有人发布了东西)我把
loginHtml
const放在
login()
func之外。还感谢Leo编辑Q。我在这里是noob,所以我在等待别人这么做
fmt.Println("username:", r.PostFormValue("username"))
fmt.Println("password:", r.PostFormValue("password"))