Skip to content

io/fs: provide backwards-compatible migration path from ioutil.ReadFile to fs.ReadFile #44286

Closed as not planned
@bouk

Description

@bouk

Hi there, I'm enjoying the go1.16 RC, I'm trying to migrate some code from using ioutil.ReadFile to fs.ReadFile while staying backwards-compatible. I'm running into two things:

  • It's not possible to make code that does something like ioutil.ReadFile("./path/to/file.txt") work with os.DirFS("."), because it rejects the leading ./
  • There is no way to have absolute paths for the same reason, because 'Open should reject attempts to open names that do not satisfy ValidPath(name)' according to the fs.FS documentation

I can get around those things by doing something like this:

// osFS is an fs.FS implementation that just passes on to os.Open
type osFS struct{}

func (c osFS) Open(name string) (fs.File, error) {
	return os.Open(name)
}

Which is technically an invalid implementation, although it's not entirely clear why those restrictions are in place. It would be nice if something like the above osFS was built-in, so there's a clear migration path from ioutil.ReadFile to fs.ReadFile.

What version of Go are you using (go version)?

$ go version go1.16rc1 darwin/arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions