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

proposal: embed: //go:embed directive to embed current file if no argument given #58130

Closed
sding3 opened this issue Jan 29, 2023 · 3 comments
Closed

Comments

@sding3
Copy link
Contributor

sding3 commented Jan 29, 2023

For a variable located in foo.go to embed the file foo.go, we need:

//go:embed foo.go
var file string

It would be convenient if the embed directive interprets the empty argument as to embed the current source file, e.g.:

//go:embed
var file string

I was looking to do documentation generation by parsing out godoc comment of a go source file that contains many configuration parameters for a program by embedding that file and parsing the AST. By allowing the embed directive embed the source file that the directive appears in when no arguments are given, I no longer have to couple the embed directive argument with that source file's filename (i.e., no longer need to update the embed directive every time the source code's filename is changed).

@gopherbot gopherbot added this to the Proposal milestone Jan 29, 2023
@ianlancetaylor
Copy link
Contributor

This seems very special purpose, and rather confusing when reading the code.

@rittneje
Copy link

I don't really understand what you are trying to do, but have you considered using go generate instead? Then you will have access to the GOFILE env var that it sets.

@sding3
Copy link
Contributor Author

sding3 commented Jan 29, 2023

This seems very special purpose, and rather confusing when reading the code.

This is not a big deal, as the file being renamed without the embed directive being updated will lead to a test failure. But currently the embed directive without argument is an error, so I figured that we might as well make the default do something useful. I suppose the current error is helpful in preventing someone accidentally including a file in the compiled binary that they didn't intend.

@sding3 sding3 closed this as completed Jan 29, 2023
@golang golang locked and limited conversation to collaborators Jan 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants