You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Embedding is really nice, but I've had multiple occasions where I've removed embeds of type string or []byte from a file and accidentally left in the _ "embed" import. goimports can't remove it because it can't know about the usage of a _ import, and go build doesn't complain about it because, again, it's a _ import and so it's technically used, kind of. This problem doesn't apply to embed.FS usage because the import is actually fully used, then.
Proposal
Add the following declarations to the embed package:
package embed
typeString=stringtypeBytes= []byte
Use of these types would not be required, but using them would allow the import to be a regular import when not using embed.FS:
package example
import (
"embed"
)
//go:embed file.txtvarfile embed.String
Making them aliases would also allow them to be used elsewhere without needing conversions, just like existing string and []byte embeds.
Alternative
I don't know if it's possible because it's comment related, but maybe a check could be added to go vet that would warn about accidentally left in _ "embed" imports without any actual //go:embed comments in the file. I think this is less ideal because it would still not be caught by go build like a normally accidentally left in import, but I'd be fine with it if other's disagree.
The text was updated successfully, but these errors were encountered:
Thanks. I thought it must have been suggested somewhere before but couldn't find it either as its own proposal or as a comment on the original proposal to add embed.
Now that go.mod truly enforces a minimum version requirement, is the underscore import actually still needed? Or can we say that if go.mod >= 1.X then the //go:embed directive alone is sufficient (unless you actually need embed.FS, etc.)?
Reasoning
Embedding is really nice, but I've had multiple occasions where I've removed embeds of type
string
or[]byte
from a file and accidentally left in the_ "embed"
import.goimports
can't remove it because it can't know about the usage of a_
import, andgo build
doesn't complain about it because, again, it's a_
import and so it's technically used, kind of. This problem doesn't apply toembed.FS
usage because the import is actually fully used, then.Proposal
Add the following declarations to the
embed
package:Use of these types would not be required, but using them would allow the import to be a regular import when not using
embed.FS
:Making them aliases would also allow them to be used elsewhere without needing conversions, just like existing
string
and[]byte
embeds.Alternative
I don't know if it's possible because it's comment related, but maybe a check could be added to
go vet
that would warn about accidentally left in_ "embed"
imports without any actual//go:embed
comments in the file. I think this is less ideal because it would still not be caught bygo build
like a normally accidentally left in import, but I'd be fine with it if other's disagree.The text was updated successfully, but these errors were encountered: