Created
May 28, 2019 03:09
-
-
Save eliben/4b1d991f9971a887f82e3b1fa535d479 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
Title: Simple HTTP server | |
URL slug: httpserver | |
Description: A simple HTTP server serving two different routes using the default request multiplexer. |
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" | |
"net/http" | |
"strings" | |
) | |
func hello(w http.ResponseWriter, req *http.Request) { | |
fmt.Fprintf(w, "hello\n") | |
} | |
func headers(w http.ResponseWriter, req *http.Request) { | |
// Loop through headers | |
for name, headers := range req.Header { | |
for _, h := range headers { | |
fmt.Fprintf(w, "%v: %v\n", strings.ToLower(name), h) | |
} | |
} | |
} | |
func main() { | |
http.HandleFunc("/hello", hello) | |
http.HandleFunc("/headers", headers) | |
http.ListenAndServe(":8080", nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment