Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: new type similar to DirFS #63810

Closed
3052 opened this issue Oct 29, 2023 · 2 comments
Closed

os: new type similar to DirFS #63810

3052 opened this issue Oct 29, 2023 · 2 comments
Labels

Comments

@3052
Copy link

3052 commented Oct 29, 2023

os.DirFS implements io/fs.ReadFileFS, but returns the interface io/fs.FS, which means you need a type assertion to make use of it:

package main

import (
   "io/fs"
   "os"
)

func main() {
   b, err := os.DirFS("hello").(fs.ReadFileFS).ReadFile("hello.txt")
   if err != nil {
      panic(err)
   }
   os.Stdout.Write(b)
}

if we just had some concrete type that implemented the interfaces, it would remove the need for a type assertion:

package main

import "os"

type Directory string

func (Directory) ReadFile(string) ([]byte, error) {
   return nil, nil
}

func main() {
   b, err := Directory("hello").ReadFile("hello.txt")
   if err != nil {
      panic(err)
   }
   os.Stdout.Write(b)
}

ideally os.DirFS would have been a type rather than a function, but it seems its now too late for that. so I would propose adding a type that implements the same items as the return value from os.DirFS. this would also make discovery simpler, because the methods would be on the package site, rather than just in the notes of the DirFS function. I dont care what name is used, I just chose Directory to have something.

@3052 3052 added the Proposal label Oct 29, 2023
@seankhliao
Copy link
Member

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Oct 29, 2023
@3052
Copy link
Author

3052 commented Oct 29, 2023

I dont get why you think its OK to just close issues like this without any discussion. the suggested option is not the same experience. it requires an extra import, and its a function rather than a method:

package main

import (
   "os"
   "io/fs"
)

func main() {
   b, err := fs.ReadFile(os.DirFS("hello"), "hello.txt")
   if err != nil {
      panic(err)
   }
   os.Stdout.Write(b)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants