go:embed question

261 views
Skip to first unread message

Endre Simo (esimov)

unread,
Oct 12, 2022, 3:37:17 AM10/12/22
to golang-nuts
I know that //go:embed directive reads the content of the embedable file at compile time. Now if that's the case I wondering if you are running a Go executable with "go run" at the same time while you are downloading like go run github.com/user/projectname@latest (which should embed an external file) why the file is not embedded?

Normally if the file you want to embed does not exits, the compiler will throw you an error telling that "no matching files found". Why is not the case when you are running without a prior download? 

I'm asking this because if I'm running the application normally the file is getting embedded correctly, but if I'm trying to build it on-the-fly with go run this is not happening? 

Marcel Huijkman

unread,
Oct 13, 2022, 1:22:26 PM10/13/22
to golang-nuts
I find this question a bit hard to understand. I think it has to do with go run and then a URL, which can't be done.
Can you rephrase it?

esimov

unread,
Oct 14, 2022, 3:17:13 AM10/14/22
to golang-nuts
Let me rephrase it: so normally when you are embedding an external file with the go://embed directive, when you are running the "go run" command it will embed it at compile time. Now, that's not the case when you are running a package using the following command: "go run github.com/user/package@latest" for example. Hope that's clear now.

Dan Kortschak

unread,
Oct 14, 2022, 4:50:18 AM10/14/22
to golan...@googlegroups.com
On Fri, 2022-10-14 at 00:17 -0700, esimov wrote:
> . Now, that's not the case when you are running a package using the
> following command: "go run github.com/user/package@latest" for
> example

Can you give an example of a real package where this doesn't work?

Tamás Gulácsi

unread,
Oct 15, 2022, 2:48:22 PM10/15/22
to golang-nuts
It won't work iff that file is not there - e.g. it is not commited into the repo.

esimov

unread,
Oct 15, 2022, 11:49:11 PM10/15/22
to golang-nuts
go run github.com/esimov/caire/cmd/caire@latest

I'm wondering is this somehow related to the fact, that the file which should be embedded is located two levels up to the root tree? The embedable file is located in the data folder, but the file effectively is not embedded in cmd/caire/main.go, but in a file called process.go which is at the same level as the data folder. 

├───cmd
│   └───caire/main.go
├───data

jake...@gmail.com

unread,
Oct 16, 2022, 10:46:15 AM10/16/22
to golang-nuts
What makes you think that go run github.com/esimov/caire/cmd/caire@latest is not embedding properly?
On my system it seems like it works fine, and the file gets embedded. I'm using go 1.19 on windows.

Perhaps you could give a more detailed example of what you are doing (actual commands) and what you see, and what you actually expect to see. If you do that in a way that allows other to replicate you problem, you might get a useful response.
Reply all
Reply to author
Forward
0 new messages