Using testing/fstest.MapFS in Production

178 views
Skip to first unread message

Jeremy French

unread,
Aug 7, 2025, 7:38:03 AM8/7/25
to golang-nuts
This is kind of a "should I" vs a "how do I" question.  

Can anyone think of a reason not to use a MapFS in production code if you want an in-memory FS?  It's in the testing package, and clearly stated that its intended use is for testing.  Is it weird to use it for not-testing?  Aside from the obvious caveats surrounding using an in-memory system in the first place.

Jason E. Aten

unread,
Aug 7, 2025, 10:08:13 PM8/7/25
to golang-nuts
I would just copy it into a separate file in your own project first--only
to avoid confusing readers who would otherwise see the "testing" package
used in production code.

I mean, its such a simple thing that of course, sure, why not? :)

type MapFS map[string]*MapFile

// A MapFile describes a single file in a [MapFS].
type MapFile struct {
Data    []byte      // file content
Mode    fs.FileMode // fs.FileInfo.Mode
ModTime time.Time   // fs.FileInfo.ModTime
Sys     any         // fs.FileInfo.Sys
}

Jake Bailey

unread,
Aug 10, 2025, 1:42:59 PM8/10/25
to golang-nuts
Many operations with MapFS require a full scan of the entire map every time, e.g. even asking for the files in a dir scans the entire map to filter it. So, if you need at least one reason not to, perf could be one!
Reply all
Reply to author
Forward
0 new messages