Run it:
w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(resp) }
func main() { http.HandleFunc("/pickup", randomPickupHandler) http.ListenAndServe(":8080", nil) }
The result: a GET /pickup endpoint that returns a random cheesy, funny, or surprisingly smooth pickup line. Create a main.go file:
func randomPickupHandler(w http.ResponseWriter, r *http.Request) { rand.Seed(time.Now().UnixNano()) line := pickupLines[rand.Intn(len(pickupLines))] resp := PickupResponse{Line: line}
package main import ( "encoding/json" "math/rand" "net/http" "time" )
type PickupResponse struct { Line string json:"line" }