Skip to content

Instantly share code, notes, and snippets.

@andreadipersio
Last active December 28, 2015 10:49
Show Gist options
  • Save andreadipersio/7488618 to your computer and use it in GitHub Desktop.
Save andreadipersio/7488618 to your computer and use it in GitHub Desktop.
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))
}
@levpaul
Copy link

levpaul commented Nov 22, 2013

I think this one is missing a defer :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment