Skip to content

proposal: spec: Add a path property to fs.DirEntry interface #75803

@Blitz-Cloud

Description

@Blitz-Cloud

Go Programming Experience

Intermediate

Other Languages Experience

js,python,c,cpp

Related Idea

  • Has this idea, or one like it, been proposed before?
  • Does this affect error handling?
  • Is this about generics?
  • Is this change backward compatible? Breaking the Go 1 compatibility guarantee is a large cost and requires a large benefit

Has this idea, or one like it, been proposed before?

No

Does this affect error handling?

No

Is this about generics?

No

Proposal

In the current state of the language, once you read the contents of a file or directory, if the path isnt saved with the contents in a user defined structure, you lose that bit of detail.

For the path to be saved, this field could be added as a string into the fs,DirEntry

// A DirEntry is an entry read from a directory
// (using the [ReadDir] function or a [ReadDirFile]'s ReadDir method).
type DirEntry interface {
	// Name returns the name of the file (or subdirectory) described by the entry.
	// This name is only the final element of the path (the base name), not the entire path.
	// For example, Name would return "hello.go" not "home/gopher/hello.go".
	Name() string

	// IsDir reports whether the entry describes a directory.
	IsDir() bool

	// Type returns the type bits for the entry.
	// The type bits are a subset of the usual FileMode bits, those returned by the FileMode.Type method.
	Type() FileMode

	// Info returns the FileInfo for the file or subdirectory described by the entry.
	// The returned FileInfo may be from the time of the original directory read
	// or from the time of the call to Info. If the file has been removed or renamed
	// since the directory read, Info may return an error satisfying errors.Is(err, ErrNotExist).
	// If the entry denotes a symbolic link, Info reports the information about the link itself,
	// not the link's target.
	Info() (FileInfo, error)
    
  // Path returns the location in the file system for this entry
  Path() string
}

Language Spec Changes

No response

Informal Change

This change will make easier to work with multiple files or directories. In case after you read a file make some edits now you can use the Path function to get location of the file where you need to write to

Is this change backward compatible?

No

I respond this way, because i am not very good at reading the low level golang functions, but from a high level and what i could understand this will not be a backward compatible proposal.

Orthogonality: How does this change interact or overlap with existing features?

No response

Would this change make Go easier or harder to learn, and why?

I think this change would not affect the learning curve for this language.

Cost Description

No response

Changes to Go ToolChain

No response

Performance Costs

No response

Prototype

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    LanguageChangeSuggested changes to the Go languageLanguageChangeReviewDiscussed by language change review committeeLibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposal

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions