【发布时间】:2016-06-03 16:12:52
【问题描述】:
我正在尝试学习 Go 网络编程,这是一个简单的网络服务器:它打印出被调用的时间。
package main
import (
"fmt"
"net/http"
)
var calls int
// HelloWorld print the times being called.
func HelloWorld(w http.ResponseWriter, r *http.Request){
calls++
fmt.Fprintf(w, "You've called me %d times", calls)
}
func main() {
fmt.Printf("Started server at http://localhost%v.\n", 5000)
http.HandleFunc("/", HelloWorld)
http.ListenAndServe(":5000", nil)
}
当我刷新页面时,我得到:
You've called me 1 times
You've called me 3 times
You've called me 5 times
....
问题:为什么是1、3、5次,而不是1、2、3...?函数HelloWorld被调用的顺序是什么?
【问题讨论】:
-
打印请求 uri 以查看从您的浏览器发送的其他请求。