Documentation
¶
Index ¶
- Variables
- func Bytes(w http.ResponseWriter, status int, data []byte) error
- func HTML(w http.ResponseWriter, status int, data string) error
- func HTMLTemplate(w http.ResponseWriter, status int, tmpl template.Template, data any) error
- func JSON[T any](w http.ResponseWriter, status int, data T) error
- func Raw(w http.ResponseWriter, status int, data []byte) error
- func SSE(w http.ResponseWriter, r *http.Request, c <-chan []byte) error
- func Stream(w http.ResponseWriter, r *http.Request, c <-chan []byte) error
- func String(w http.ResponseWriter, status int, data string) error
- func StringTemplate(w http.ResponseWriter, status int, tmpl template.Template, data any) error
- func XML(w http.ResponseWriter, status int, data any) error
Constants ¶
This section is empty.
Variables ¶
var ErrNonFlushableWriter = errors.New("non-flushable response writer")
ErrNonFlushableWriter is returned when the http.ResponseWriter does not implement the http.Flusher interface, which is required for streaming responses.
Functions ¶
func Bytes ¶
func Bytes(w http.ResponseWriter, status int, data []byte) error
Bytes writes binary data to the response writer with the appropriate Content-Type header for binary content (application/octet-stream). This is useful for serving files, images, or any binary data that should be treated as a download or binary stream.
Parameters:
- w: The HTTP response writer
- status: The HTTP status code to set
- data: The binary data to write to the response
func HTML ¶
func HTML(w http.ResponseWriter, status int, data string) error
HTML writes HTML content to the response writer with the appropriate Content-Type header for HTML (text/html). This is used for serving static HTML content or HTML strings that have been pre-generated.
Parameters:
- w: The HTTP response writer
- status: The HTTP status code to set
- data: The HTML string to write to the response
func HTMLTemplate ¶
HTMLTemplate executes an HTML template with the provided data and writes the result as HTML to the response writer. The Content-Type is set to text/html. This is the standard way to serve dynamic HTML pages in web applications using Go's html/template package.
Parameters:
- w: The HTTP response writer
- status: The HTTP status code to set (note: this is set after template execution)
- tmpl: The HTML template to execute
- data: The data to pass to the template for execution
func JSON ¶
func JSON[T any](w http.ResponseWriter, status int, data T) error
JSON serializes the given data to JSON format and writes it to the response writer. It automatically sets the Content-Type header to application/json and uses Go's json.Encoder for efficient streaming serialization. This is the standard way to serve JSON API responses.
The function uses generics to provide type safety for the input data while still accepting any serializable type.
Parameters:
- w: The HTTP response writer
- status: The HTTP status code to set
- data: The data to serialize as JSON (must be JSON-serializable)
func Raw ¶
func Raw(w http.ResponseWriter, status int, data []byte) error
Raw writes raw byte data to the response writer with the specified status code. It does not set any Content-Type header, allowing the caller full control over the response format. This is the most basic response function that other response functions build upon.
Parameters:
- w: The HTTP response writer
- status: The HTTP status code to set
- data: The raw byte data to write to the response
func SSE ¶
SSE (Server-Sent Events) sends data from a channel to an HTTP client using the Server-Sent Events protocol. It sets the appropriate Content-Type header for SSE and delegates to the Stream function for the actual streaming logic. This is useful for implementing real-time updates to web browsers that support the EventSource API.
Parameters:
- w: The HTTP response writer
- r: The HTTP request (used for context cancellation)
- c: A receive-only channel providing byte slices to stream as SSE data
func Stream ¶
Stream sends data from a channel to an HTTP client in real-time using HTTP streaming. It sets appropriate headers for streaming and flushes data as it becomes available. The function returns when the channel is closed (graceful shutdown) or when the request context is canceled. If the ResponseWriter doesn't support flushing, it returns ErrNonFlushableWriter.
Parameters:
- w: The HTTP response writer
- r: The HTTP request (used for context cancellation)
- c: A receive-only channel providing byte slices to stream
func String ¶
func String(w http.ResponseWriter, status int, data string) error
String writes plain text data to the response writer with the appropriate Content-Type header for text content (text/plain; charset=utf-8). This is ideal for serving plain text responses, logs, or simple text data.
Parameters:
- w: The HTTP response writer
- status: The HTTP status code to set
- data: The string data to write to the response
func StringTemplate ¶
StringTemplate executes a text template with the provided data and writes the result as plain text to the response writer. The Content-Type is set to text/plain; charset=utf-8. This is useful for generating plain text content from templates, such as emails or configuration files.
Parameters:
- w: The HTTP response writer
- status: The HTTP status code to set (note: this is set after template execution)
- tmpl: The text template to execute
- data: The data to pass to the template for execution
func XML ¶
func XML(w http.ResponseWriter, status int, data any) error
XML serializes the given data to XML format and writes it to the response writer. It automatically sets the Content-Type header to application/xml and uses Go's xml.Encoder for streaming serialization. This is useful for serving XML API responses or RSS feeds.
The data should be a struct with appropriate xml tags for proper marshaling, or implement xml.Marshaler interface for custom serialization.
Parameters:
- w: The HTTP response writer
- status: The HTTP status code to set
- data: The data to serialize as XML (must be XML-serializable)
Types ¶
This section is empty.