In this example, we'll see how to stream responses using the Echo web framework in Go. Streaming responses allows the server to send data to the client as it becomes available, rather than waiting for all the data to be ready before sending it.
package main
import (
"net/http"
"time"
"github.com/labstack/echo/v4"
)
func streamResponse(c echo.Context) error {
// Set the HTTP headers for chunked transfer encoding
c.Response().Header().Set(echo.HeaderContentType, "text/event-stream")
c.Response().WriteHeader(http.StatusOK)
// Stream data
for i := 0; i < 10; i++ {
// Send the data
if _, err := c.Response().Write([]byte("data: Chunk " + strconv.Itoa(i) + "\n\n")); err != nil {
return err
}
c.Response().Flush() // Flush the data to the client
time.Sleep(time.Second) // Simulate delay
}
return nil
}
func main() {
e := echo.New()
e.GET("/stream", streamResponse)
e.Start(":8080")
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?