Chapter 15 - Web Apps
Exercise 1: A Simple Web App
Here’s the start of a web app that simulates rolling a six-sided die. When a browser makes a request for the /roll
path, the app will respond with a random number from 1 to 6. Each refresh of the page will generate a new random number.
- Within
rollHandler
:- Call
rollDie
. - Convert the return value to a
string
. You may want to consult the documentation for the"strconv"
package’sItoa
function. - Convert the string to a slice of bytes, and store it in a variable named
body
.
- Call
- Within
main
, set up therollHandler
function to handle all requests with a path of"/roll"
. See the documentation for the"net/http"
package’sHandleFunc
function.