Skip to content

proposal: embed: add String and Bytes aliases #62417

@DeedleFake

Description

@DeedleFake

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, 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
type String = string
type Bytes = []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.txt
var file 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions