1.16 beta 1 - Getting an error trying to use the "embed" package

168 views
Skip to first unread message

Amit Saha

unread,
Dec 18, 2020, 7:38:10 AM12/18/20
to golang-nuts
Hi all, has anyone tried using the “embed” package in the 1.16 beta 1 release?

My data.go file looks as:

package main

import "embed"

//go:embed templates/main.go.tmpl
var tmplMainGo []byte


When I build the program, I get this:

~/go/bin/go1.16beta1 build
# github.com/amitsaha/go-embed
./data.go:3:8: imported and not used: “embed”


Now, I understand the error, but what’s the fix? I blindly tried to remove the import, but then I get:

# github.com/amitsaha/go-embed
./data.go:5:3: //go:embed only allowed in Go files that import “embed"

What am I doing wrong?

My test code is here: https://github.com/amitsaha/go-embed

Thank you.

Best Regards,
Amit.



Volker Dobler

unread,
Dec 18, 2020, 7:43:06 AM12/18/20
to golang-nuts
Use

    import _ "embed"  // note the _

Your code does not  use package embed. A comment does
not qualify as usage. As yous must import it for //go:embed
comments to work you have to use a "side-effects-only"-import.

V.

Amit Saha

unread,
Dec 18, 2020, 7:50:33 AM12/18/20
to Volker Dobler, golang-nuts


On Fri, 18 Dec 2020, 11:44 pm Volker Dobler, <dr.volke...@gmail.com> wrote:
Use

    import _ "embed"  // note the _

Your code does not  use package embed. A comment does
not qualify as usage. As yous must import it for //go:embed
comments to work you have to use a "side-effects-only"-import.

Ah yes. This should be mentioned in the package documentation. Probably it is not.

Thank you.

V.

On Friday, 18 December 2020 at 13:38:10 UTC+1 amits...@gmail.com wrote:
Hi all, has anyone tried using the “embed” package in the 1.16 beta 1 release?

My data.go file looks as:

package main

import "embed"

//go:embed templates/main.go.tmpl
var tmplMainGo []byte


When I build the program, I get this:

~/go/bin/go1.16beta1 build
# github.com/amitsaha/go-embed
./data.go:3:8: imported and not used: “embed”


Now, I understand the error, but what’s the fix? I blindly tried to remove the import, but then I get:

# github.com/amitsaha/go-embed
./data.go:5:3: //go:embed only allowed in Go files that import “embed"

What am I doing wrong?

My test code is here: https://github.com/amitsaha/go-embed

Thank you.

Best Regards,
Amit.



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/0552d6cc-93be-49f6-96c0-cd1679854460n%40googlegroups.com.

Ian Lance Taylor

unread,
Dec 18, 2020, 1:46:38 PM12/18/20
to Amit Saha, Volker Dobler, golang-nuts
On Fri, Dec 18, 2020 at 4:50 AM Amit Saha <amits...@gmail.com> wrote:
>
> On Fri, 18 Dec 2020, 11:44 pm Volker Dobler, <dr.volke...@gmail.com> wrote:
>>
>> Use
>>
>> import _ "embed" // note the _
>>
>> Your code does not use package embed. A comment does
>> not qualify as usage. As yous must import it for //go:embed
>> comments to work you have to use a "side-effects-only"-import.
>
>
> Ah yes. This should be mentioned in the package documentation. Probably it is not.

This is fairly likely to change before the final release. See
https://golang.org/issue/43217.

Ian

Amit Saha

unread,
Dec 18, 2020, 5:16:07 PM12/18/20
to Ian Lance Taylor, Volker Dobler, golang-nuts
Thank you


Ian
Reply all
Reply to author
Forward
0 new messages