Last active
March 5, 2016 09:19
-
-
Save shijuvar/eae8e5baa28ae84674ce to your computer and use it in GitHub Desktop.
A simple HTTP server in Go
This file contains 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" | |
"io" | |
"log" | |
"net/http" | |
) | |
func helloHandler(res http.ResponseWriter, req *http.Request) { | |
res.Header().Set( | |
"Content-Type", | |
"text/html", | |
) | |
io.WriteString( | |
res, | |
`<doctype html> | |
<html> | |
<head> | |
<title>Hello Gopher</title> | |
</head> | |
<body> | |
Hello Gopher </br> | |
It is really awesome that both Docker and Kubernetes are written in Go! | |
</body> | |
</html>`, | |
) | |
} | |
func defaultHandler(w http.ResponseWriter, r *http.Request) { | |
fmt.Fprintf(w, "Go web app powered by Docker") | |
} | |
func main() { | |
http.HandleFunc("/", defaultHandler) | |
http.HandleFunc("/hello", helloHandler) | |
err := http.ListenAndServe(":8080", nil) | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment