Skip to content

os: add Root.ReadFile & Root.WriteFile #73126

Closed

Description

@neild

I propose adding ReadFile and WriteFile methods to *os.Root:

package os

// ReadFile reads the named file in the root and returns the contents.
// See [ReadFile] for more details.
func (r *Root) ReadFile(name string) ([]byte, error)

// WriteFile writes data to the named file in the root, creating it if necessary.
// See [WriteFile] for more details.
func (r *Root) WriteFile(name string, data []byte, perm FileMode) error

These are purely convenience functions (as are os.ReadFile and os.WriteFile). I propose adding them because:

  1. os.ReadFile and os.WriteFile are very convenient convenience functions, and it's a shame not to have an equivalent that works with os.Root.
  2. The io/fs package not only includes a ReadFile convenience function, it includes a ReadFileFS variant of FS containing a ReadFile method. I think that points at this being something we consider core filesystem functionality.

Activity

  1. added this to the Proposal milestone on Apr 1, 2025
  2. gabyhelp commented on Apr 1, 2025

    @gabyhelp
  3. added
    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a tool
    on Apr 1, 2025
  4. changed the title [-]proposal: add os.Root.ReadFile/os.Root.WriteFile[/-] [+]proposal: os: add Root.ReadFile & Root.WriteFile[/+] on Apr 1, 2025
  5. irsl commented on Apr 4, 2025

    @irsl
    Contributor

    Could we have an os level shortcut methods as well (similar to os.OpenInRoot)? By having that, one wouldn't need to bother with closing the root for one-time operations.

    Without that, the following oneliner:

    data, err:= os.ReadFile(filepath.Join(dir, file))
    

    Would be around 6:

    r, err:= os.OpenRoot(dir)
    if err != nil {
       log.Fatalf(...)
    }
    defer r.Close()
    data, err:= r.ReadFile(file)
    
  6. neild commented on Apr 4, 2025

    @neild
    ContributorAuthor

    I don't think we want to have top-level functions for every Root operation. That'd be a lot of new functions.

    Filed #73168 with a proposal for a single top-level InRoot function that would allow using any Root method as a one-liner.

  7. moved this to Incoming in Proposalson May 14, 2025
  8. aclements commented on May 14, 2025

    @aclements
    Member

    Based on the discussion above, this proposal seems like a likely accept.
    — aclements for the proposal review group

    The proposal is given in #73126 (comment).

  9. moved this from Incoming to Likely Accept in Proposalson May 14, 2025
  10. gopherbot commented on May 19, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/674315 mentions this issue: os: add Root.ReadFile and Root.WriteFile

  11. moved this from Likely Accept to Accepted in Proposalson May 21, 2025
  12. aclements commented on May 21, 2025

    @aclements
    Member

    No change in consensus, so accepted. 🎉
    This issue now tracks the work of implementing the proposal.
    — aclements for the proposal review group

    The proposal is given in #73126 (comment).

  13. changed the title [-]proposal: os: add Root.ReadFile & Root.WriteFile[/-] [+]os: add Root.ReadFile & Root.WriteFile[/+] on May 21, 2025
  14. modified the milestones: Proposal, Backlog on May 21, 2025
  15. aclements commented on Jun 10, 2025

    @aclements
    Member

    @neild , should this issue be closed now?

  16. neild commented on Jun 10, 2025

    @neild
    ContributorAuthor

    Implemented, closing.

  17. locked and limited conversation to collaborators on Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions