Last active
December 28, 2015 10:49
-
-
Save andreadipersio/7488618 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" | |
"strconv" | |
) | |
var ( | |
port int | |
) | |
func rootHandler(w http.ResponseWriter, r *http.Request) { | |
ua := r.Header.Get("User-Agent") | |
msg := fmt.Sprintf("Your user agent is: %v\n", ua) | |
w.Header().Set("Content-Type", "text/plain") | |
w.Header().Set("Content-Length", strconv.Itoa(len(msg))) | |
fmt.Fprint(w, msg) | |
} | |
func init() { | |
flag.IntVar(&port, "port", 8080, "HTTP Server Port") | |
flag.Parse() | |
} | |
func main() { | |
httpAddr := fmt.Sprintf(":%v", port) | |
log.Printf("Listening to %v", httpAddr) | |
http.HandleFunc("/", rootHandler) | |
log.Fatal(http.ListenAndServe(httpAddr, nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think this one is missing a defer :P