Standard library / net/http
http.NewFileTransportFS()
Create a transport serving seekable files from a supplied fs.FS. It ignores URL host; never expose unrestricted local files to untrusted URLs. Expected stdout: 'hello\n'.
Formal Syntax & Structure
go syntaxgo
func NewFileTransportFS(fsys fs.FS) RoundTripperParameters & Attributes
| Name | Type | Description |
|---|---|---|
| fsys | fs.FS | File system containing seekable files. |
Return Value
RoundTripper
Operation result; check errors and validity indicators when returned.
Working Example
Example Snippetgo
package main
import ( "fmt" "io" "net/http" "testing/fstest")
func main() { rt := http.NewFileTransportFS(fstest.MapFS{"demo.txt": {Data: []byte("hello")}}) r := must(rt.RoundTrip(must(http.NewRequest("GET", "file:///demo.txt", nil)))) defer r.Body.Close() fmt.Println(string(must(io.ReadAll(r.Body))))}
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) }}