英文:
Go web server : cannot find anything on http://localhost:8080/handler
问题
我正在尝试使用Go学习Web编程。
我从一个简单的“hello world” web服务器开始:
package main
import "fmt"
import "net/http"
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
当我在浏览器中访问
http://localhost:8080/handler
时,浏览器似乎找不到任何内容,什么都没有发生。这可能是什么原因?
英文:
I am trying to learn web programming with Go.
I stared out with a simple "hello world" web server:
package main
import "fmt"
import "net/http"
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, world")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
And when I go to
http://localhost:8080/handler
in the browser, the browser can´t seem to find anything and nothing happens. What could be the reason for this?
答案1
得分: 4
你将处理程序映射到服务器的根目录(“/”)。
在浏览器中这样调用它
http://localhost:8080/
如果你想将一个服务映射到特定的名称,你可以这样做:
http.HandleFunc("/something", handler)
然后你可以在浏览器中输入以下内容:
http://localhost:8080/something
英文:
You mapped your handler to the root ("/"
) of your server.
Call it like this in your browser
http://localhost:8080/
If you want to map a service to a specific name you can do this :
http.HandleFunc("/something", handler)
Then you would type this in your browser :
http://localhost:8080/something
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论