我的代码与gowiki中的代码相同
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
Run Code Online (Sandbox Code Playgroud)
但是,在我构建并运行此程序后,它立即退出而没有阻塞,因此当我尝试http://localhost:8080/monkey从Chrome 访问时,我没有得到任何响应.
环境:Ubuntu 14(在Windows7上的VirtualBox中)
为什么?
One*_*One 17
检查从中返回的错误 ListenAndServe
func main() {
http.HandleFunc("/", handler)
fmt.Println(http.ListenAndServe(":8080", nil))
}
Run Code Online (Sandbox Code Playgroud)
http.ListenAndServe函数返回符合error接口的对象。如果调用没有阻塞,则肯定意味着发生了某种错误。最受欢迎的是:
8080或0.0.0.0接口上绑定套接字| 归档时间: |
|
| 查看次数: |
4007 次 |
| 最近记录: |