Last active
December 28, 2015 11:29
-
-
Save andreadipersio/7493430 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"flag" | |
"net/http" | |
) | |
var ( | |
port int | |
) | |
func visitServer(visitChan chan bool) { | |
var visits int | |
for { | |
if <-visitChan { | |
visits++ | |
log.Printf("%v visits", visits) | |
} | |
} | |
} | |
type withChanHandler struct { | |
visitChan chan bool | |
} | |
func (h *withChanHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
h.visitChan <-true | |
fmt.Fprint(w, "Hello, World!") | |
} | |
func init() { | |
flag.IntVar(&port, "port", 8080, "HTTP Server Port") | |
flag.Parse() | |
} | |
func main() { | |
var visitChan = make(chan bool) | |
go visitServer(visitChan) | |
httpAddr := fmt.Sprintf(":%v", port) | |
log.Printf("Listening to %v", httpAddr) | |
http.Handle("/", &withChanHandler{visitChan,}) | |
log.Fatal(http.ListenAndServe(httpAddr, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome!