无法在http://localhost:8080/handler上找到任何内容的Go Web服务器。

huangapple go评论87阅读模式
英文:

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

huangapple
  • 本文由 发表于 2012年9月19日 19:45:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/12494033.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定