Created
July 23, 2018 11:12
-
-
Save fwojciec/4a0a5b4eb38111eae58e64d9d104b62f to your computer and use it in GitHub Desktop.
My solution to "Exercise Hello, parameter" from Section 03 of Francesc Campoy's Go Web Workshop
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
// https://github.com/campoy/go-web-workshop/blob/master/section03/README.md | |
// this version is able to handle multiple parameters with the same name | |
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"github.com/gorilla/mux" | |
) | |
func friendHandler(w http.ResponseWriter, r *http.Request) { | |
if err := r.ParseForm(); err != nil { | |
log.Fatal(err) | |
} | |
names := r.Form["name"] | |
if len(names) == 0 { | |
names = append(names, "friend") | |
} | |
for _, n := range names { | |
fmt.Fprintf(w, "Hello, %s!\n", n) | |
} | |
} | |
func main() { | |
r := mux.NewRouter() | |
r.HandleFunc("/hello", friendHandler) | |
http.Handle("/", r) | |
if err := http.ListenAndServe("127.0.0.1:8080", nil); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment