Options for writable fs.FS extension (Create and Append)

90 views
Skip to first unread message

ben...@gmail.com

unread,
Jul 27, 2026, 11:14:29 PM (2 days ago) Jul 27
to golang-nuts
Hi folks,

I'm considering (in draft PR https://github.com/benhoyt/goawk/pull/296) adding the option to GoAWK to allow customizing the file read/write operations that it performs when reading files, creating new files with AWK's ">" operator, or appending to them with AWK's ">>" operator.

I'd like it to be shaped like io/fs.FS. However, that only supports read-only files (Open returns an fs.File). So my thought is to have an interface extension like so:

type WriteFS interface {
fs.FS
Create(name string) (io.WriteCloser, error)
Append(name string) (io.WriteCloser, error)
}

Which are all the operations GoAWK needs.

Then if you pass a regular fs.FS (via Config.FileSystem) it'll only allow reads, but if you psas something that implements WriteFS as defined above, it'll support read and write.

I notice that some other libraries define WriteFS as:

type WriteFS interface {
    fs.FS
    OpenFile(name string, flag int, perm os.FileMode) (io.WriteCloser, error)
}

That fits nicely with os.OpenFile, but it pulls in a whole lot of POSIX-specific stuff and generality with the "flag" and "perm" arguments, when I only need Create and Append very specifically.

Does what I've proposed seem reasonable? Any better ideas for handling this?

-Ben

Ian Lance Taylor

unread,
Jul 28, 2026, 1:32:07 AM (yesterday) Jul 28
to ben...@gmail.com, golang-nuts
On Mon, Jul 27, 2026 at 8:15 PM ben...@gmail.com <ben...@gmail.com> wrote:
>
> I'd like it to be shaped like io/fs.FS. However, that only supports read-only files (Open returns an fs.File). So my thought is to have an interface extension like so:
>
> type WriteFS interface {
> fs.FS
> Create(name string) (io.WriteCloser, error)
> Append(name string) (io.WriteCloser, error)
> }

There is some previous discussion at https://go.dev/issue/45757.

TL;DR: It's complicated.

Ian

Ben Hoyt

unread,
Jul 28, 2026, 4:09:59 AM (yesterday) Jul 28
to Ian Lance Taylor, golang-nuts
Thanks, Ian. That was an interesting thread -- yes, complicated! However, it seems that at least Create() was suggested several times with the same basic shape, so I guess I'm aiming in the right direction. My Append() seems to be novel, but I think "fits". It avoids the interface having to think about OS-level mode/perm/flags.

Def Ceb

unread,
Jul 28, 2026, 4:25:06 AM (yesterday) Jul 28
to golang-nuts
Personally, I think Append() would be a bit too high level for a *filesystem* interface, and that something more like FUSE would make more sense. Maybe with some Capabilities() method to signal what functionality you actually implement.
It does get a bit too muddy the more I think about it though.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CAL9jXCH3%3DREus%3DYZeO53zuMsTekdstX5MKCrva_eK7nWnemA7g%40mail.gmail.com.

Mohamed Zane Attahri

unread,
Jul 28, 2026, 4:51:25 AM (yesterday) Jul 28
to Def Ceb, golang-nuts
Feels like the addition of os.Root to stdlib somewhat standardized what a writable FS looks like. 

On Jul 28, 2026, at 4:24 AM, Def Ceb <mikk....@gmail.com> wrote:


Reply all
Reply to author
Forward
0 new messages