Problem using embed in Go 1.16

228 views
Skip to first unread message

Tong Sun

unread,
Oct 10, 2021, 12:12:07 PM10/10/21
to golang-nuts
Hi,

I'm having problem using embed with Go 1.16. I'm getting ether

- //go:embed only allowed in Go files that import "embed"
- or imported and not used: "embed"

The test code is published at

here are the details:

If I remove the "embed" Go import, I'll get:

$ go build
./embeding.go:17:3: //go:embed only allowed in Go files that import "embed"

If I add back the "embed" Go import, I'll get:

$ go build
./embeding.go:11:2: imported and not used: "embed"

$ go version
go version go1.16.6 linux/amd64

$ lsb_release -a 
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux bullseye/sid
Release:        10.10
Codename:       buster

The main file:

-------------------------------
package main

import (
"embed"
"flag"
"fmt"
"os"
)

//go:embed cpuArch.txt
var cpuArch string

func usage() {
// Fprintf allows us to print to a specifed file handle or stream
fmt.Fprintf(os.Stderr, "\nUsage: %s [flags] file [path ...]\n\n",
"CommandLineFlag") // os.Args[0]
flag.PrintDefaults()
os.Exit(0)
}

func main() {
fmt.Printf("Before parsing the flags\n")
fmt.Printf("CPU: '%s'\n", cpuArch)

flag.StringVar(&cpuArch, "cpu", "AMD64", "CPU Arch")

flag.Usage = usage
flag.Parse()

// There is also a mandatory non-flag arguments
if len(flag.Args()) < 1 {
usage()
}

fmt.Printf("\nAfter parsing the flags\n")
fmt.Printf("CPU: '%s'\n", cpuArch)

}
-------------------------------


Sean Liao

unread,
Oct 10, 2021, 12:20:42 PM10/10/21
to golang-nuts
import _ "embed"

It means to import it for side effects only

Tong Sun

unread,
Oct 10, 2021, 12:23:27 PM10/10/21
to golang-nuts
Ahh, found it but you beat me to it.

Apparently the sample code that I'm referring to,
https://github.com/treeverse/blogs/blob/master/mypostcard/main.go
is outdated. 

Thanks!

Tong Sun

unread,
Oct 10, 2021, 12:36:05 PM10/10/21
to golang-nuts
On Sunday, October 10, 2021 at 12:23:27 PM UTC-4 Tong Sun wrote:
Ahh, found it but you beat me to it.

Apparently the sample code that I'm referring to,
https://github.com/treeverse/blogs/blob/master/mypostcard/main.go
is outdated. 

Ops, I was wrong, again. 

Marcin Romaszewicz

unread,
Oct 10, 2021, 1:57:22 PM10/10/21
to Tong Sun, golang-nuts
If you're not directly using symbols from embed, import it with an _ alias, eg:

import _ "embed"

-- Marcin

--
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/4124bd7e-f5b1-4259-a433-05959021d05cn%40googlegroups.com.

Thomas Frössman

unread,
Oct 12, 2021, 5:46:49 PM10/12/21
to Sean Liao, golang-nuts
import (
_ "embed"
   ...
)

--
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.


--
Thomas Frössman
Reply all
Reply to author
Forward
0 new messages