Copy the code below to grocery.go and list.html files, both saved in the same directory. Then fill in the missing code in both files. When a browser visits http://localhost:8080/fruit, your app should respond with this HTML (the spacing doesn’t have to match): apples oranges pears And when a browser visits http://localhost:8080/meat, your app should respond with this HTML: chicken beef lamb Try running your finished app, and visit the above URLs in your browser! grocery.go package main import ( "html/template" "log" "net/http" ) func check(err error) { if err != nil { log.Fatal(err) } } func writeList(writer http.ResponseWriter, list []string) { // YOUR CODE HERE: Use the template library to parse the contents // of the "list.html" file. You'll get a template value and an // error value. // Pass the error value to the "check" function. // Now call the "Execute" method on the template. It should write // its output to the "writer" parameter, and it should use the // "list" parameter as its data value. // You'll get another error value back from "Execute", which // should be passed to "check". } func fruitHandler(writer http.ResponseWriter, request *http.Request) { writeList(writer, []string{"apples", "oranges", "pears"}) } func meatHandler(writer http.ResponseWriter, request *http.Request) { writeList(writer, []string{"chicken", "beef", "lamb"}) } func main() { http.HandleFunc("/fruit", fruitHandler) http.HandleFunc("/meat", meatHandler) err := http.ListenAndServe("localhost:8080", nil) log.Fatal(err) } list.html
Copy the code below to grocery.go and list.html files, both saved in the same directory. Then fill in the missing code in both files.
When a browser visits http://localhost:8080/fruit, your app should respond with this HTML (the spacing doesn’t have to match):
<html>
<ul>
<li>apples</li>
<li>oranges</li>
<li>pears</li>
</ul>
</html>
And when a browser visits http://localhost:8080/meat, your app should respond with this HTML:
<html>
<ul>
<li>chicken</li>
<li>beef</li>
<li>lamb</li>
</ul>
</html>
Try running your finished app, and visit the above URLs in your browser!
grocery.go
package main
import (
"html/template"
"log"
"net/http"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func writeList(writer http.ResponseWriter, list []string) {
// YOUR CODE HERE: Use the template library to parse the contents
// of the "list.html" file. You'll get a template value and an
// error value.
// Pass the error value to the "check" function.
// Now call the "Execute" method on the template. It should write
// its output to the "writer" parameter, and it should use the
// "list" parameter as its data value.
// You'll get another error value back from "Execute", which
// should be passed to "check".
}
func fruitHandler(writer http.ResponseWriter, request *http.Request) {
writeList(writer, []string{"apples", "oranges", "pears"})
}
func meatHandler(writer http.ResponseWriter, request *http.Request) {
writeList(writer, []string{"chicken", "beef", "lamb"})
}
func main() {
http.HandleFunc("/fruit", fruitHandler)
http.HandleFunc("/meat", meatHandler)
err := http.ListenAndServe("localhost:8080", nil)
log.Fatal(err)
}
list.html
<html>
<ul>
<!-- YOUR CODE HERE:
The data value passed to the template should be a slice.
Loop over each element in the slice, and include them in
the output, enclosed in <li></li> tags. -->
</ul>
</html>
Trending now
This is a popular solution!
Step by step
Solved in 3 steps