proposal: os: convenience function for one-off Root operations #73168
Labels
LibraryProposal
Issues describing a requested change to the Go standard library or x/ libraries, but not to a tool
Proposal
Milestone
I propose adding the following function:
os.Root
(#67002) supports performing filesystem operations within the context of some directory.Performing a single operation in a Root is substantially more verbose than doing so outside of one. Compare:
If the
Root
will be reused for many operations, the additional overhead of opening and closing it is not so significant. For a single operation, however, it's enough to be annoying.We have an
os.OpenInRoot
convenience function for opening files, but this covers only one operation. We could add similar functions for other operations (os.CreateInRoot
,os.RemoveInRoot
, etc.), but that would be a lot of new functions.This has come up in the context of #73126, which proposes adding
Root.ReadFile
andRoot.WriteFile
methods: #73126 (comment) asks if we can have top-level convenience functions as well, since opening and closing the root for short operations is inconvenient.I've also had this raised in a Google-internal discussion: We have a linter which recommends using the
github.com/google/safeopen
package for certain operations (recommending, for example, thatos.Create(filepath.Join(a, b))
could besafeopen.CreateBeneath(a, b)
). We're reluctant to change the linter to recommendos.Root
in cases where a one-liner becomes several lines opening and closing a Root.The
InRoot
function allows performing operations in a root with slightly less verbosity than the non-rooted equivalent usingfilepath.Join
. To continue the above example:The text was updated successfully, but these errors were encountered: