For simple things, you can fire up a goroutine to do the "something else" 
after the request finishes. For example, I've used this before to kick off 
sending an email to a customer in the background (you'll want to 
handle/report errors somehow though):

package main

import (
"fmt"
"log"
"net/http"
"time"
)

func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "sending email in the background")
go sendEmail()
// response is sent, but sendEmail goroutine continues
})
log.Print("listening on http://localhost:8080";)
http.ListenAndServe(":8080", nil)
}

func sendEmail() {
log.Printf("would send an email here")
time.Sleep(time.Second)
log.Printf("done sending email")
}

On Thursday, July 6, 2023 at 2:03:47 AM UTC+12 alex-coder wrote:

Hi All !

So, http server looks like is a request / response processing.
But in case it is nesessary to do something else after the response has 
been sent to the client, how to do it properly ?
Is there any example to read ?

Thank you.


-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/795ab574-4437-43de-909b-7cceedb5556dn%40googlegroups.com.

Reply via email to