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
| Name | Type | Description |
|---|---|---|
| w | ResponseWriter | Destination writer. |
| req | *Request | HTTP request associated with the operation. |
| name | string | Name of the requested cookie or field. |
| modtime | time.Time | Input value of the declared type; see the usage example. |
| content | io.ReadSeeker | Input 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) }}