I write a piece of code to record the num of request.
package main
import (
"log"
"net/http"
"runtime"
"sync/atomic"
)
var count int32 = 0
func test(w http.ResponseWriter, r *http.Request) {
count = atomic.LoadInt32(&count)
atomic.AddInt32(&count, 1)
log.Println("count:", count)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
http.HandleFunc("/", test)
http.ListenAndServe(":8080", nil)
}
I have considered the condition of concurrency, so I use the atomic pacakge. I test the code via the apache ab tool
ab -c 400 -n 1000 http://localhost:8080/
the result is correct: result However, someone said he got the 1004 or other number on his computer, I have tested many times of the code, but the result is correct on my computer, is there wrong with my way? I am new to go, Thanks in advance.