Mastering-Go-Second-Edition/ch12/atomWWW.go
Mihalis Tsoukalos 0775f15607 Atomic in WWW
2019-05-01 17:07:06 +03:00

27 lines
525 B
Go

package main
import (
"fmt"
"net/http"
"runtime"
"sync/atomic"
)
var count int32
func handleAll(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&count, 1)
}
func getCounter(w http.ResponseWriter, r *http.Request) {
temp := atomic.LoadInt32(&count)
fmt.Println("Count:", temp)
fmt.Fprintf(w, "<h1 align=\"center\">%d</h1>", count)
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU() - 1)
http.HandleFunc("/getCounter", getCounter)
http.HandleFunc("/", handleAll)
http.ListenAndServe(":8080", nil)
}