Skip to content

Instantly share code, notes, and snippets.

@rodkranz
Created December 19, 2016 14:04
Show Gist options
  • Save rodkranz/d6c85eca7850a09a991089bb49a628e6 to your computer and use it in GitHub Desktop.
Save rodkranz/d6c85eca7850a09a991089bb49a628e6 to your computer and use it in GitHub Desktop.
Server count incrementer in go
// Copyright 2016 Kranz. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"net/http"
"fmt"
"runtime"
"log"
"os"
"os/signal"
)
var (
i int64
c chan int64
)
func HelloWorld (w http.ResponseWriter, r *http.Request) {
c <- 1
fmt.Fprintf(w, "Hello guest number %v\n", i)
}
func init() {
runtime.GOMAXPROCS(runtime.NumCPU())
c = make(chan int64)
go func() {
for n := range c {
i += n
}
}()
go func() {
s := make(chan os.Signal, 1)
signal.Notify(s, os.Interrupt)
<-s
close(c)
fmt.Printf("Total of visitors are [%v]", i)
}()
}
func main() {
http.HandleFunc("/", HelloWorld)
log.Println("Listen server http://0.0.0.0:9090/")
log.Fatal(http.ListenAndServe(":9090", nil))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment