Standard library / net/http

http.ServeContent()

Serves seekable content with range and conditional request handling; the supplied name helps identify media type. Expected stdout: '206 ell bytes 1-3/5\n'.

Formal Syntax & Structure

go syntaxgo
func ServeContent(w ResponseWriter, req *Request, name string, modtime time.Time, content io.ReadSeeker)

Parameters & Attributes

NameTypeDescription
wResponseWriterDestination writer.
req*RequestHTTP request associated with the operation.
namestringName of the requested cookie or field.
modtimetime.TimeInput value of the declared type; see the usage example.
contentio.ReadSeekerInput value of the declared type; see the usage example.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http"  "net/http/httptest"  "strings"  "time")
func main() {  w := httptest.NewRecorder()  r := httptest.NewRequest("GET", "/demo.txt", nil)  r.Header.Set("Range", "bytes=1-3")  http.ServeContent(w, r, "demo.txt", time.Time{}, strings.NewReader("hello"))  fmt.Println(w.Code, w.Body.String(), w.Header().Get("Content-Range"))}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference