我正在尝试用GO语言制作简单的服务器,为什么它不加载页面而是继续显示错误消息?
我正在遵循一个教程,并且我已经对所有内容进行了交叉检查,似乎并没有工作.............................................................................................. .....................................................................
import (
"fmt"
"log"
"net/http"
)
func formHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
fmt.Fprintf(w, "POST request succesfull")
name := r.FormValue("name")
address := r.FormValue("address")
fmt.Fprintf(w, "Name = %s\n", name)
fmt.Fprintf(w, "Address = %s\n", address)
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/hello" {
http.Error(w, "404 not found", http.StatusNotFound)
return
}
if r.Method != "Get" {
http.Error(w, "method is not supported", http.StatusNotFound)
return
}
fmt.Fprintf(w, "hello!")
}
func main() {
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/form", formHandler)
http.HandleFunc("/hello", helloHandler)
fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
I am following a tutorial and I have literally crosschecked everything, It seems not to be working still ....................................................................
import (
"fmt"
"log"
"net/http"
)
func formHandler(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
fmt.Fprintf(w, "ParseForm() err: %v", err)
return
}
fmt.Fprintf(w, "POST request succesfull")
name := r.FormValue("name")
address := r.FormValue("address")
fmt.Fprintf(w, "Name = %s\n", name)
fmt.Fprintf(w, "Address = %s\n", address)
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/hello" {
http.Error(w, "404 not found", http.StatusNotFound)
return
}
if r.Method != "Get" {
http.Error(w, "method is not supported", http.StatusNotFound)
return
}
fmt.Fprintf(w, "hello!")
}
func main() {
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/form", formHandler)
http.HandleFunc("/hello", helloHandler)
fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在您的代码中只看到一个问题,但它在
/Hello
处理程序中。您正在检查是否使用r.Method!=“ get”
,但是http方法在大写中,因此您应该使用r.method!=“ get”
甚至更好,使用给定常数r.method!= http.methodget
。这为我解决了/Hello
问题:现在,
/form
处理问题不在您的代码中,但碰巧您正在尝试加载form.html
而不是调用form
,如果您在项目中的static> static
subfolder中有一个名为form.html
的文件,则可以工作。这样(这是一个非常极简主义示例):这是因为您已经在
http.handle(“/”,Fileserver)
中处理静态文件。我不知道您正在关注的教程,但似乎是意图。直接尝试
表单
的另一个选项,没有HTML可能会使用curl
:还有其他工具。 HTML应该可以训练。
I see only one issue in your code, but it is in the
/hello
handler. You are checking if it is a GET usingr.Method != "Get"
, but HTTP methods are in uppercase, so you should user.Method != "GET"
, or even better, use the given constantr.Method != http.MethodGet
. That solves the/hello
issue for me:Now, the
/form
handle issue is not in your code, but happens that you are trying to loadform.html
instead of callingform
, which can work if you have a file calledform.html
in thestatic
subfolder in your project, like this (which is a very minimalist example):That works because you are already handling static files in
http.Handle("/", fileServer)
. I don't know the tutorial you are following, but looks like that was the intention.Another option to try
form
directly, without HTML could be using something likecurl
:There are other tools. The HTML one should be fine for training.
我可以看到您的
HelloHandler
函数在比较请求方法时,您应该将其与“ get”而不是“ get”进行比较。有关更多信息,请查看: https://pkg.go.go.dev/net/net/http# pkg-contants
我在本地运行了此代码,它在本地为我运行良好。我上面提到的这种方法更改。您可以发布确切的错误吗?
I can see there is one more error in your
helloHandler
function when you compare the request Method, you should compare it with "GET" and not "Get".For more info look at: https://pkg.go.dev/net/http#pkg-constants
I ran this code locally it is working fine locally for me. With this Method change I mentioned above. Can you post the exact error you are getting?