dimanche 11 août 2019

Code design for handle funcs in go web app

I'm learning go and ran into some design issues while developing web app. The app has main route "/" where user can submit a simple form. With those form values I am calling external API and unmarshaling response into some struct. Now from here I want to make another call based on retrieved values to another external API and I'm not sure what's the proper way of doing this. Here is a snippet for better understandment:

func main() {
    http.HandleFunc("/", mainHandler)
    log.Fatal(http.ListenAndServe(":8080", nil))
}

func mainHandler(w http.ResponseWriter, r *http.Request) {
    //renders form template
    //makes post and retrieves data from api
    //here with retrieved data I want to make another call to different API,
    // but mainHandler would get too big and complex. I'm not sure how should I pass this data to
    // another handler or redirect to another handler with this data.
}

Aucun commentaire:

Enregistrer un commentaire